Paz*_*r80 5 python text input python-3.x
好吧,所以我使用了很多输入命令,我明白在Python2中我可以这样做:
text = raw_input ('Text here')
Run Code Online (Sandbox Code Playgroud)
但是现在我使用Python 3,我想知道它们之间有什么区别:
text = input('Text here')
Run Code Online (Sandbox Code Playgroud)
和:
text = eval(input('Text here'))
Run Code Online (Sandbox Code Playgroud)
我何时必须使用其中一个?
在Python 3.x中,raw_input成为了input和Python 2.x中的input被删除.所以,在3.x中执行此操作:
text = input('Text here')
Run Code Online (Sandbox Code Playgroud)
你基本上是在2.x做这个:
text = raw_input('Text here')
Run Code Online (Sandbox Code Playgroud)
在3.x中执行此操作:
text = eval(input('Text here'))
Run Code Online (Sandbox Code Playgroud)
与在2.x中执行此操作相同:
text = input('Text here')
Run Code Online (Sandbox Code Playgroud)
以下是Python Docs的快速摘要:
PEP 3111:
raw_input()改名为input().也就是说,新input()函数从中读取一行sys.stdin并返回它,并删除尾随换行符.EOFError如果输入提前终止,它会引发 .要获得旧的行为input(),请使用eval(input()).