輸入
- python2版本中
咱們在銀行ATM機器前取錢時,肯定需要輸入密碼,對不?
那麼怎樣才能讓程序知道咱們剛剛輸入的是什麼呢??
大家應該知道了,如果要完成ATM機取錢這件事情,需要先從鍵盤中輸入一個數據,然後用一個變量來保存,是不是很好理解啊
1.1 raw_input()
在Python中,獲取鍵盤輸入的數據的方法是採用 raw_input 函數(至於什麼是函數,咱們以後的章節中講解),那麼這個 raw_input 怎麼用呢?
看如下示例:
password = raw_input("請輸入密碼:")
print('您剛剛輸入的密碼是:%d' % password)
運行結果:
注意:
raw_input()的小括號中放入的是,提示信息,用來在獲取數據之前給用戶的一個簡單提示
raw_input()在從鍵盤獲取了數據以後,會存放到等號右邊的變量中
raw_input()會把用戶輸入的任何值都作爲字符串來對待
1.2 input()
input()函數與raw_input()類似,但其接受的輸入必須是表達式。
>> a = input()
123
>> a
123
>> type(a)
<type 'int'>
>> a = input()
abc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>> a = input()
"abc"
>> a
'abc'
>> type(a)
<type 'str'>
>> a = input()
1+3
>> a
4
>> a = input()
"abc"+"def"
>> a
'abcdef'
>> value = 100
>> a = input()
value
>> a
100
input()接受表達式輸入,並把表達式的結果賦值給等號左邊的變量
- python3版本中
沒有raw_input()函數,只有input()
並且 python3中的input與python2中的raw_input()功能一樣