python 3中的输入字符串

Den*_*iev 11 python string eval input python-3.x

我有字符串变量测试,在Python 2.7中这很好用.

test = raw_input("enter the test") 
print test
Run Code Online (Sandbox Code Playgroud)

但是在Python 3.3中.我用

test = input("enter the test") 
print test
Run Code Online (Sandbox Code Playgroud)

使用输入字符串test,我收到一条错误消息

Traceback(最近一次调用最后一次):

文件"/home/ananiev/PycharmProjects/PigLatin/main.py",

第5行,在test = input("输入测试")

NameError中的文件"",第1行:名称'sdas'未定义

Cai*_*von 14

您正在使用Python 2解释器运行Python 3代码.如果你不是,你的print陈述会SyntaxError在它提示你输入之前抛出.

结果是你正在使用Python 2 input,它会尝试eval你的输入(大概sdas),发现它是无效的Python,并且死了.


Noc*_*tua 6

我要说你需要的代码是:

test = input("enter the test")
print(test)
Run Code Online (Sandbox Code Playgroud)

否则,由于语法错误,它根本不应该运行.该print函数需要在python 3中使用括号.但是,我无法重现您的错误.你确定是那些导致错误的行吗?