为了账号安全,请及时绑定邮箱和手机立即绑定

python:NameError 名称输入

python:NameError 名称输入

心有法竹 2021-09-11 16:29:36
我试图在 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我在“atom”、“sublime”、“visual studio code”中测试我的代码
查看完整描述

1 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

这是因为你在 python 2 上,所以input在 python 中基本上是eval(input(...))在 python 3 中,所以它将输入作为代码,而不是字符串,所以必须raw_input在 python 2 中使用:


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")


查看完整回答
反对 回复 2021-09-11
  • 1 回答
  • 0 关注
  • 400 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号