python - 为什么 IDLE shell 中出现这个 NameError?
问题描述
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
当我运行它...
What is your name?
>>> bn
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
bn
NameError: name 'bn' is not defined
版本
>>> import sys; print(sys.version)
3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)]
解决方案
>>> bn
这意味着您bn
在解释器 shell 上键入,而不是在前面的输入函数中键入......
你可能想写这个而不是先打印
myName = input('What is your name? ')
您的错误也可能与 Python2 相关
例子
$ cat /tmp/app.py
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
$ python2 /tmp/app.py
What is your name?
bn
Traceback (most recent call last):
File "/tmp/app.py", line 2, in <module>
myName = input()
File "<string>", line 1, in <module>
NameError: name 'bn' is not defined
$ python3 /tmp/app.py
What is your name?
bn
It is good to meet you, bn
The length of your name is:
2
如果你想使用 Python2,你需要使用raw_input()
asinput()
隐式调用eval()
输入的值
推荐阅读
- devexpress - DevExpress XAF:获取子集合的集合
- html - 将绝对定位的 DIV 与 IE11 中 TD 元素内的左值垂直对齐
- knex.js - 无法读取未定义的属性“事务”
- unit-testing - 64位除法的单元测试
- reactjs - Redux 表单 React 中的两个提交按钮
- react-native - 如何在本机反应中显示图像的宽度和高度
- mysql - MySQL MATCH AGAINST 没有给出结果
- javascript - 谷歌图表背景
- mysql - 如何使用 mySQL 数据库中的数据填充 Google 折线图。(单个文件/内联解决方案)
- flutter - Flutter 无法运行项目