首页 文章

python:NameError名称输入[重复]

提问于
浏览
1

这个问题在这里已有答案:

我试图在python中获得一些输入,我不知道我的问题是什么

name = input("What's your name?")
age = int(input("How old are you?"))
year = str((100 - age) + 2018)
print("Hello "+ name + ",in " + year + "you'll be 100 y.o")

当我使用我的名字作为“shayan”的输入时,那就出现了:

name = input("What's your name? ")
File "<string>", line 1, in <module>
NameError: name 'shayan' is not defined

我将我的代码改为“原子”,“崇高”,“视觉工作室代码”

1 回答

  • 0

    它在python 2上是's because you',所以python中的 input 基本上是 eval(input(...)) 在python 3中,所以它将输入作为代码,而不是字符串,所以必须在python 2中使用 raw_input

    name = raw_input("What's your name?")
    age = input("How old are you?")
    year = str((100 - age) + 2018)
    print("Hello "+ name + ",in " + year + "you'll be 100 y.o")
    

相关问题