Python报错File “「string」“, line 1, in 「module」 NameError: name ‘q‘ is not defined

2023-05-16

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':
        break
    second_number = input("second number:")
    if second_number =='q':
        break
    answer = 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':
        break
    second_number = raw_input("second number:")
    if second_number =='q':
        break
    answer = 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
2

First number:q

参考链接:
python 报错:str=input() File “”, line 1, in NameError: name ‘hello’ is not defined

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python报错File “「string」“, line 1, in 「module」 NameError: name ‘q‘ is not defined 的相关文章

  • 是否可以在 Sphinx 中隐藏 Python 函数参数?

    假设我有以下函数 该函数记录在Numpydoc 风格 https github com numpy numpy blob master doc HOWTO DOCUMENT rst txt 并且文档是自动生成的Sphinx http sph
  • Highcharts 奇怪的分组行为

    我正在使用延迟加载 http www highcharts com stock demo lazy loading加载 OHLC 数据的方法 在服务器端 我使用 Python MySQL 并有 4 个包含 OHLC 数据的表 时间间隔为 5
  • 导入错误:没有名为“wordcloud”的模块

    我正在努力将 wordcloud 安装到我的环境中 这是我正在运行的代码 import os import matplotlib pyplot as plt from wordcloud import WordCloud 我收到以下错误 I
  • 为什么我不能“string”.print()?

    我的理解print 在 Python 和 Ruby 以及其他语言 中 它是字符串 或其他类型 上的方法 因为它的语法非常常用 打印 嗨 works 那么为什么不呢 hi print 在 Python 中或 hi print在红宝石工作 当你
  • 比较两个文本文件并计算差异

    我一直在尝试在Python中比较两个文本文件 本质上我想打开它们并一次比较一个字符 如果字符不同 则向计数器添加1 然后显示该值 这是我到目前为止所拥有的 usr bin env python diff 0 import random im
  • 来自数据框 groupby 的条形图

    import pandas as pd import numpy as np import matplotlib pyplot as plt df pd read csv arrests csv df df replace np nan 0
  • 使用 python-docx 在 docx 文件中查找所有“正常”样式且字体大小不是 11 的文本

    到目前为止我的实现 from docx api import Document import pandas as pd from docx shared import Pt texts sizes document Document new
  • 获取 HTML 代码的结构

    我正在使用 BeautifulSoup4 我很好奇是否有一个函数可以返回 HTML 代码的结构 有序标签 这是一个例子 h1 Simple example h1 p This is a simple example of html page
  • 用于匹配重复子字符串的单个js正则表达式?

    假设我有一个字符串 例如 where is mummy where is daddy 我想用空字符串替换任何一组重复子字符串 所以在这种情况下where and is元素将被删除 结果字符串将是 mummy daddy 我想知道是否有任何单
  • 如何在 Python 中将 EXR 文件的 float16 转换为 uint8

    我正在使用 OpenEXR 读取 Python 中的 EXR 文件 我有带有半数据 float16 的 R G 和 B 通道 我尝试使用 Numpy 将数据从 float16 转换为 uint8 0 255 颜色 但没有成功 rCh get
  • 使用 .map() 在 pandas DataFrame 中高效创建附加列

    我正在分析形状与以下示例类似的数据集 我有两种不同类型的数据 abc数据和xyz data abc1 abc2 abc3 xyz1 xyz2 xyz3 0 1 2 2 2 1 2 1 2 1 1 2 1 1 2 2 2 1 2 2 2 3
  • Learning_rate 不是合法参数

    我正在尝试通过实现 GridSearchCV 来测试我的模型 但我似乎无法在 GridSearch 中添加学习率和动量作为参数 每当我尝试通过添加这些代码来执行代码时 我都会收到错误 这是我创建的模型 def define model op
  • 为什么 tesseract 无法从这个简单的图像中读取文本?

    我在 pytesseract 上阅读了大量的帖子 但我无法让它从一个简单的图像中读取文本 它返回一个空字符串 这是图像 我尝试过缩放它 灰度化它 调整对比度 阈值 模糊 以及其他帖子中所说的一切 但我的问题是我不知道 OCR 想要更好地工作
  • Python中的MariaDB连接器无法连接到远程服务器

    我使用与远程 Mariadb 服务器的连接已有几个月了 今天 无法再通过 macOS 上的 python mariadb 模块和 mariadb 连接器建立连接 基本安装如下 brew install mariadb connector c
  • 在Python中将罗马数字转换为整数

    根据 user2486 所说 这是我当前的代码 def romanMap map M 1000 CM 900 D 500 CD 400 C 100 XC 90 L 50 XL 40 X 10 IX 9 V 5 V 4 I 1 return
  • 用户的完整 UNIX 用户名

    想知道您是否知道是否有一种巧妙的方法可以从 shell 获取完整的用户名 示例 如果我的 UNIX 用户名是 froyo 那么我想获取我的全名 在本例中 如系统中注册的那样 froyo Abhishek Pratap Finger 命令可以
  • Python 可以替代 Java 小程序吗?

    除了制作用于物理模拟 如抛射运动 重力等 的教育性 Java 小程序之外 还有其他选择吗 如果你想让它在浏览器中运行 你可以使用PyJamas http pyjs org 这是一个 Python 到 Javascript 的编译器和工具集
  • Matplotlib 渲染日期、图像的问题

    我在使用 conda forge 的 Matplotlib v 3 1 3 和 python 3 7 时遇到问题 我拥有 Matplotlib 所需的所有依赖项 当我输入这段代码时 它应该可以工作 我得到了泼溅艺术 它基于此 YouTube
  • 需要一个从 yaml 文件中提取内容并输出为 csv 文件的脚本

    我对 python 很陌生 但我很感激您帮助指导我创建一个简单的脚本 该脚本读取一堆 yaml 文件 同一目录中的大约 300 个文件 并从 yaml 文件并将其转换为 csv yaml 文件中内容的示例 code 9313 degrees
  • 将自定义属性添加到 Tk 小部件

    我的主要目标是向小部件添加隐藏标签或字符串之类的内容 以在其上保存简短信息 我想到创建一个新的自定义 Button 类 在本例中我需要按钮 它继承所有旧选项 这是代码 form tkinter import class NButton Bu

随机推荐