所謂輸入,就是用代碼獲取用戶通過鍵盤輸入的資訊,
例如:去銀行取錢,在 ATM 上輸入密碼,
在 Python 中,如果要獲取用戶在鍵盤上的輸入資訊,需要使用到input()函式,
函式input()讓程式暫停運行,等待用戶輸入一些文本,獲取用戶輸入后,Python將其存盤在一個變數中,以方便使用,
input() 函式總是以字串的形式來處理用戶輸入的內容,所以用戶輸入的內容可以包含任何字符,
str = input(tipmsg)
- str 表示一個字串型別的變數,input會將讀取到的字串放入 str 中,
- tipmsg 表示提示資訊,它會顯示在控制臺上,告訴用戶應該輸入什么樣的內容;如果不寫tipmsg,就不會有任何提示資訊,
【實體】input() 函式的簡單使用:
a = input("Enter a number: ")
b = input("Enter another number: ")
print("aType: ", type(a))
print("bType: ", type(b))
result = a + b
print("resultValue: ", result)
print("resultType: ", type(result))
運行結果示例:
Enter a number: 100↙
Enter another number: 45↙
aType: <class 'str'>
bType: <class 'str'>
resultValue: 10045
resultType: <class 'str'>
↙表示按下回車鍵,按下回車鍵后 input() 讀取就結束了,
本例中我們輸入了兩個整數,希望計算出它們的和,但是事與愿違,Python只是它們當成了字串,+ 起到了拼接字串的作用,而不是求和的作用,
所以,一定要謹記,input()函式獲取的資訊都是字串型別,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67457.html
標籤:Python
上一篇:Python操作MySQL
下一篇:python計算字母出現次數
