input和raw_input的區別


input會假設用戶輸入的是合法的Python表達式
raw_input會把所有的輸入當作原始數據,然后將其放入字符串中。

在最新的版本之中,input可以直接使用,替代了raw_input.

在2.7的版本中
>>> input('Enter you age: ')
Enter you age: kebi                                      input假設你輸入的是字符串,但是字符串需要帶引號啊
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'kebi' is not defined
直接輸入數字會報錯。
加上引號就可以了

 

>>> input('Enter you age: ')
Enter you age: 'kebi'
'kebi'                 加上引號就沒事了

 

>>> raw_input('Enter you age: ')
Enter you age: kebi                     使用raw_input就不會存在這個問題了
'kebi'

在3.6的版本中
>>> raw_input('you name:')                         raw_input直接就不存在了,統一使用input
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined

>>> input('you name:')
you name:kebi
'kebi'

 

總結:不得不說這是一次進步。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM