###### tags: `chapter 4` `Python` # 4-2 資料輸入`input()` # 在前面的實作中通常都是用自己定義的參數當作基準,如果我們想要讓客戶(client)能夠依照自己的數據輸入資料的話,我們可以使用`input()`函數。 ```python= name = input() score = input() print(name) print(score) ``` 執行上面的程式,發現可以依照自己的意願輸入資料了,但是,我要怎麼知道現在輸入的資料是哪一項呢?如果有10種不同的資料要輸入怎麼辦? 這時候,可以將提示字放在`()`裡面。 ```python= name = input("請輸入名字:") score = input("請輸入成績:") print(name) print(score) ``` ![](https://i.imgur.com/3ZKSUFC.png) 經由`input()`函式輸入進來的所有資料都是字串類型哦!如果想對`score`做調分,記得要先轉換成整數or浮點數型別。 ![](https://i.imgur.com/HDgYaUS.png) 根據上面的注意小框框,如果我們想要處理學生的成績,算出平均值等數據的話,應該要先做型別的轉換,或許會長這個樣子。 ```python= name = input("請輸入名字:") math = input("請輸入數學成績:") chinese = input("請輸入中文成績:") english = input("請輸入英文成績:") math = int(math) chinese = int(chinese) english = int(english) average = (math + chinese + english) // 3 print("{} 的期中考平均是 {} 分".format(name, average)) # 請輸入名字:John # 請輸入數學成績:100 # 請輸入中文成績:80 # 請輸入英文成績:90 # John 的期中考平均是 90 分 ```