【學習筆記】python2和python3的input()


python2中的input()只接受變量作為傳入值,非變量內容會報錯。

 1 >>> user=input("Enter your name:")
 2 Enter your name:Kaito
 3 Traceback (most recent call last):
 4   File "<stdin>", line 1, in <module>
 5   File "<string>", line 1, in <module>
 6 NameError: name 'Kaito' is not defined
 7 >>> 
 8 >>> name="Kaito"
 9 >>> user=input("Enter your name:")
10 Enter your name:name
11 >>> print(user)
12 Kaito

所以在python2中,我們用raw_input()來代替input()的功能來鍵入內容。

1 >>> user=raw_input("Enter your name:")
2 Enter your name:Kaito
3 >>> print(user)
4 Kaito

而python3的input()可以說是取代了python2的raw_input()。

1 >>> user=input("Enter your name:")
2 Enter your name:Kaito
3 >>> print(user)
4 Kaito

 


免責聲明!

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



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