python - python: NameError 名称输入
问题描述
我试图在 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”中尝试了我的代码
解决方案
这是因为您使用的是 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")
推荐阅读
- sql - 第一个字母 - 每个单词大写,业务类型除外
- docker - 如何保持 docker 容器正常运行?
- maven - Maven 插件何时使用当前目录中的 POM?
- numpy - 在数组中查找那些“关闭”的元素
- python - 如何使用 pandas 为与多部分索引的一部分匹配的所有行设置值
- r - 如何使用R查找矩阵中发生变化的次数
- android - 如何在 Kotlin 中重构这个 Android TakePicture 代码?
- r - 如何找出我的数据集的哪些观察结果已用于 R (nlme) 中的传销?
- angular - 从打字稿访问 MVC 控制器方法
- c# - Physics.Linecast() 未检测到地面