【发布时间】:2019-02-15 10:34:48
【问题描述】:
我正在使用语法
name = input(' Enter name: ')
print("hello " + name)
但每次我尝试使用其中包含名称变量的字符串时,它都会显示“名称未定义”。
【问题讨论】:
标签: python python-2.7
我正在使用语法
name = input(' Enter name: ')
print("hello " + name)
但每次我尝试使用其中包含名称变量的字符串时,它都会显示“名称未定义”。
【问题讨论】:
标签: python python-2.7
在 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 函数。 (没有自动尝试评估字符串的函数;提供这样的函数被认为是一个糟糕的设计选择。)
【讨论】:
Python 2,使用raw_input:
name = raw_input(' Enter name: ')
Python 2 的 input 函数基本上是 Python 3 的 eval(input(..))
Python 3、python 2 的 input 被移除,
只保留重命名为input的raw_input
【讨论】: