我正在使用语法
name = input(' Enter name: ')
print("hello " + name)
但是每次我尝试使用包含name变量的字符串时,都会显示“ name is undefined”。
答案 0 :(得分:2)
在Python 2.x中,input
尝试将输入评估为Python表达式。您想使用raw_input
函数:
# input('...') is equivalent to eval(raw_input('...'))
name = raw_input(' Enter name: ')
但是,如果您刚刚起步,则应该使用Python 3,其中input
函数的行为类似于Python 2的raw_input
函数。 (没有函数会自动尝试评估字符串;提供这样的函数被认为是错误的设计选择。)
答案 1 :(得分:1)
Python 2,使用raw_input
:
name = raw_input(' Enter name: ')
Python 2的input
函数基本上是Python 3的eval(input(..))
Python 3,Python 2的input
已删除,
仅保留已重命名为raw_input
的{{1}}