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相关推荐

  1. 树莓派(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 ...

  2. Python 报错TypeError: expected string or bytes-like object

    问题描述:Python 中报错TypeError: expected string or bytes-like object 解决方法:根据报错上方的提示信息,大体猜测到可能出错的具体地方.出现该报错 ...

  3. centos使用yum命令安装报错 File /usr/bin/yum, line 30 except KeyboardInterrupt, e:

    原因 centos7使用yum命令安装报错如下: File "/usr/bin/yum", line 30except KeyboardInterrupt, e:^ SyntaxE ...

  4. 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 ...

  5. 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 ...

  6. 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的解决 ...

  7. 安装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_ ...

  8. python 报错 EOL while scanning string literal

    python 报错 EOL while scanning string literal 翻译为:不完整字符串 路径中未转义的反斜杠引起 解决办法: 第一个右引号改为英文,使用'r'开头,字符串不应以' ...

  9. Python报错SyntaxError: EOL while scanning string literal

    Python报错SyntaxError: EOL while scanning string literal 是由于引号没有成对出现,定位报错语句,检查引号即可

最新文章

  1. Linux常用开发环境软件-jdk安装
  2. nginx FastCGI错误Primary script unknown解决办法
  3. Java通过Pattern类使用正则表达式
  4. Windows Server 2012 R2上Compact/Shrink vhdx
  5. openshift_在WildFly和OpenShift上的WebSocket聊天
  6. js几种常见排序的实现
  7. bootstrap之glyphicon字体图标
  8. 项目管理学习总结(12)——世界上最好的十条研发管理经验
  9. 如何解决MFC读取文件在EditControl中显示是乱码的问题
  10. 批量导出数据和全部导出到Excel(详细)和mybatis 中 Foreach的用法
  11. springboot毕设项目超市仓库管理系统15g4i(java+VUE+Mybatis+Maven+Mysql)
  12. labview温度采集系统(数据保存到excel)
  13. FastAdmin 目录权限设置
  14. sin^k(x)的积分
  15. python傅里叶逆变换_C# 傅里叶变换 逆变换 调用MathNet包|简明python教程|python入门|python教程...
  16. Python 数值计算库scipy 统计模块stats用法入门
  17. 艾伦·麦席森·图灵的传奇人生
  18. gitlab展示CHANGELOG
  19. 深圳大学软工专硕数二英二改为数一英一,计算机专硕改为英一
  20. 误删了win10下面的winsock和winsock2

热门文章

  1. 22-08-25 MySQL高级(03)MySQL索引、索引演绎、适合加索引的情况、执行计划Explain各字段解释
  2. 【matlab】进制转化
  3. 本地电脑关联云服务器
  4. siverlight网络分房间斗地主4出牌规则算法
  5. 3Dslicer_Editor(2)
  6. 速卖通开店注册流程介绍及常见问题解答
  7. 获奖之后,感慨万分……
  8. android FM搜索频率流程
  9. 『算法』哨兵查找算法
  10. 封装调用OEM7F7.EXE的接口