Python报错File “「string」“, line 1, in 「module」 NameError: name ‘q‘ is not defined
Python报错File “”, line 1, in NameError: name ‘q’ is not defined
笔者运行环境:Python 2.7.17
print ("give me tow numbers,and i will divid them.")
print ("enter 'q' to quit.")while True:first_number = input("\nFirst number:")if first_number == 'q':breaksecond_number = input("second number:")if second_number =='q':breakanswer = int(first_number) / int(second_number)print(answer)
当输入“q”时会报错,File “”, line 1, in 。NameError: name ‘q’ is not defined
运行结果报错:
li@ubuntu:~/桌面/python$ python -u "/home/li/桌面/python/aa99.py"
give me tow numbers,and i will divid them.
enter 'q' to quit.First number:q
Traceback (most recent call last):File "/home/li/桌面/python/aa99.py", line 7, in <module>first_number = input("\nFirst number:")File "<string>", line 1, in <module>
NameError: name 'q' is not defined
原因为Python版本问题,以上用法为Python3.x以上,Python3一下需要修改:
修改后:
将input
修改为raw_input
print ("give me tow numbers,and i will divid them.")
print ("enter 'q' to quit.")while True:first_number = raw_input("\nFirst number:")if first_number == 'q':breaksecond_number = raw_input("second number:")if second_number =='q':breakanswer = int(first_number) / int(second_number)print(answer)
##运行结果 不报错
此时就不会报错了
li@ubuntu:~/桌面/python$ python -u "/home/li/桌面/python/aa99.py"
give me tow numbers,and i will divid them.
enter 'q' to quit.First number:5
second number:2
2First number:q
参考链接:
python 报错:str=input() File “”, line 1, in NameError: name ‘hello’ is not defined
Python报错File “「string」“, line 1, in 「module」 NameError: name ‘q‘ is not defined相关推荐
- 树莓派(Raspberry Pi 3) centos7使用yum命令报错File /usr/bin/yum, line 30 except KeyboardInterrupt, e:...
使用yum命令报错 File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid ...
- Python 报错TypeError: expected string or bytes-like object
问题描述:Python 中报错TypeError: expected string or bytes-like object 解决方法:根据报错上方的提示信息,大体猜测到可能出错的具体地方.出现该报错 ...
- centos使用yum命令安装报错 File /usr/bin/yum, line 30 except KeyboardInterrupt, e:
原因 centos7使用yum命令安装报错如下: File "/usr/bin/yum", line 30except KeyboardInterrupt, e:^ SyntaxE ...
- pycharm使用ssh连接服务器(ubuntu)跑代码报错:“sudo+ssh: ……bash: line 0: cd: xxx/code: No such file or directory”
pycharm使用ssh连接服务器(ubuntu)跑代码报错:"sudo+ssh: --bash: line 0: cd: xxx/code: No such file or directo ...
- python 报错 SyntaxError: Non-ASCII character ‘\xe6‘ in file E:\PycharmProjects\lianxi\re 111.py on lin
python 报错 SyntaxError: Non-ASCII character '\xe6' in file E:\PycharmProjects\lianxi\re 111.py on lin ...
- python报错:xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 1的解决办法
本篇文章主要讲解,python报错:xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 1的解决 ...
- 安装Phoenix时,执行./splline.py报错File “./sqlline.py“, line 25, in <module> import phoenix_utils File
安装Phoenix时,执行./splline.py报错File "./sqlline.py", line 25, in <module> import phoenix_ ...
- python 报错 EOL while scanning string literal
python 报错 EOL while scanning string literal 翻译为:不完整字符串 路径中未转义的反斜杠引起 解决办法: 第一个右引号改为英文,使用'r'开头,字符串不应以' ...
- Python报错SyntaxError: EOL while scanning string literal
Python报错SyntaxError: EOL while scanning string literal 是由于引号没有成对出现,定位报错语句,检查引号即可
最新文章
- Linux常用开发环境软件-jdk安装
- nginx FastCGI错误Primary script unknown解决办法
- Java通过Pattern类使用正则表达式
- Windows Server 2012 R2上Compact/Shrink vhdx
- openshift_在WildFly和OpenShift上的WebSocket聊天
- js几种常见排序的实现
- bootstrap之glyphicon字体图标
- 项目管理学习总结(12)——世界上最好的十条研发管理经验
- 如何解决MFC读取文件在EditControl中显示是乱码的问题
- 批量导出数据和全部导出到Excel(详细)和mybatis 中 Foreach的用法
- springboot毕设项目超市仓库管理系统15g4i(java+VUE+Mybatis+Maven+Mysql)
- labview温度采集系统(数据保存到excel)
- FastAdmin 目录权限设置
- sin^k(x)的积分
- python傅里叶逆变换_C# 傅里叶变换 逆变换 调用MathNet包|简明python教程|python入门|python教程...
- Python 数值计算库scipy 统计模块stats用法入门
- 艾伦·麦席森·图灵的传奇人生
- gitlab展示CHANGELOG
- 深圳大学软工专硕数二英二改为数一英一,计算机专硕改为英一
- 误删了win10下面的winsock和winsock2