用戶互動
python3 使用 input 輸入
python2 使用 raw_input 輸入
# 輸入姓名 name = input("請輸入姓名:") print(type(name)) # 輸入密碼 passwd = input("請輸入密碼:") print(type(passwd))
結果:
請輸入姓名:小明 <class 'str'> 請輸入密碼:123 <class 'str'>
注意:無論我們輸入的值是數字型別、字串型別、串列型別,input的接收值都是字串型別,
轉換:
# 輸入密碼 passwd = int(input("請輸入密碼:")) print(type(passwd))
結果:
請輸入密碼:123 <class 'int'>
===============================================
Python 2:
raw_input() 獲取用戶輸入的內容并將其作為字串傳回,
input()首先采取raw_input(),然后執行eval()它,
主要區別在于input()期望語法正確的python陳述句raw_input()不需要,
Python 3:
raw_input()被重命名為input()現在input()回傳確切的字串,
老input()被洗掉了,
如果你想使用舊的input(),意味著你需要將用戶輸入作為python陳述句進行評估,你必須使用它來手動完成eval(input()),
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124155.html
標籤:Python
