鱼C论坛

 找回密码
 立即注册
查看: 1503|回复: 14

[已解决]數學乘法執行問題-一直無法驗算結果

[复制链接]
发表于 2021-2-24 20:54:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想求助 各位大大,最近想利用 pyhton 做一個 數學的乘法題目,但參考網路上所寫的,自己在按「q」執行時要統計結果時,一直出現 以下訊息
question = input('Please input your answer:(q for exit)')
File "<string>", line 1, in <module>
NameError: name 'q' is not defined
實在搞不懂,想請各位大大幫忙該問題出在那裡呢??


以下為語法
==================
import random

count = 0       #答题總數
right = 0       #正確數量

while True:
    a = random.randint(0,9)
    b = random.randint(0,9)
    print('%d*%d=' %(a,b))
    question = input('Please input your answer:(q for exit)')
    result = a * b
    if question == int(result):
        print('OK!')
        right += 1
        count += 1
    elif question == 'q':
        break
    else:
        print('Failed!')
        count +=1

percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))

最佳答案
2021-2-26 08:46:49
我运行的都没问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-2-24 20:56:56 | 显示全部楼层
改成这样:
  1. import random

  2. count = 0       #答题總數
  3. right = 0       #正確數量

  4. while True:
  5.     a = random.randint(0,9)
  6.     b = random.randint(0,9)
  7.     print('%d*%d=' %(a,b))
  8.     question = input('Please input your answer:(q for exit)')
  9.     if question == 'q':
  10.         break
  11.     result = a * b
  12.     if question.isdigit() and int(question) == result:
  13.         print('OK!')
  14.         right += 1
  15.         count += 1
  16.     else:
  17.         print('Failed!')
  18.         count += 1

  19. percent = right / count
  20. print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-24 21:00:30 | 显示全部楼层
  1. import random

  2. count = 0       #答题總數
  3. right = 0       #正確數量

  4. while True:
  5.     a = random.randint(0,9)
  6.     b = random.randint(0,9)
  7.     print('%d*%d=' %(a,b))
  8.     question = input('Please input your answer:(q for exit)')
  9.     result = a * b

  10.     if question == 'q':
  11.         break
  12.     elif int(question) == int(result):
  13.         print('OK!')
  14.         right += 1
  15.         count += 1
  16.     else:
  17.         print('Failed!')
  18.         count +=1

  19. percent = right / count
  20. print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码

  1. 3*0=
  2. Please input your answer:(q for exit)0
  3. OK!
  4. 3*0=
  5. Please input your answer:(q for exit)0
  6. OK!
  7. 9*5=
  8. Please input your answer:(q for exit)45
  9. OK!
  10. 3*9=
  11. Please input your answer:(q for exit)20
  12. Failed!
  13. 6*4=
  14. Please input your answer:(q for exit)q
  15. 测试结束,共回答4道题,正确个数为3,正确率为75.00%
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-25 09:24:32 | 显示全部楼层

  1. #原代码
  2. import random

  3. count = 0       #答题總數
  4. right = 0       #正確數量

  5. while True:
  6.     a = random.randint(0,9)
  7.     b = random.randint(0,9)
  8.     print('%d*%d=' %(a,b))
  9.     question = input('Please input your answer:(q for exit)')
  10.     result = a * b
  11.     if question == int(result):#错误
  12.         print('OK!')
  13.         right += 1
  14.         count += 1
  15.     elif question == 'q':
  16.         break
  17.     else:
  18.         print('Failed!')
  19.         count +=1

  20. percent = right / count
  21. print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码

以下是解决问题之后的结果

  1. #改编后的代码
  2. import random

  3. count = 0       #答题總數
  4. right = 0       #正確數量

  5. while True:
  6.     a = random.randint(0,9)
  7.     b = random.randint(0,9)
  8.     print('%d*%d=' %(a,b))
  9.     question = input('Please input your answer:(q for exit)')
  10.     result = a * b
  11.     if question != 'q' and int(question) == result:#判断question是否不等于'q'的同时与正确答案相等,question != 'q'为了防止出错
  12.         print('OK!')
  13.         right += 1
  14.         count += 1
  15.     elif question == 'q':
  16.         break
  17.     else:
  18.         print('Failed!')
  19.         count +=1
  20.         
  21. percent = right / count
  22. print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-25 19:10:35 | 显示全部楼层

Please input your answer:(q for exit)245
Failed!
1*7=
Please input your answer:(q for exit)7
OK!
1*9=
Please input your answer:(q for exit)9
OK!
2*1=
Please input your answer:(q for exit)q
=========================
Traceback (most recent call last):
  File "nice50.py", line 11, in <module>
    question = input('Please input your answer:(q for exit)')
  File "<string>", line 1, in <module>
NameError: name 'q' is not defined

當我執行時,按 q要跳出時會發生這個錯誤??

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-25 19:14:11 | 显示全部楼层

感謝大大回覆,但我執行後會發生
7*3=
Please input your answer:(q for exit)21
Traceback (most recent call last):
  File "nice50.py", line 15, in <module>
    if question.isdigit() and int(question) == result:
AttributeError: 'int' object has no attribute 'isdigit'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-25 19:16:32 | 显示全部楼层
H.E.G. 发表于 2021-2-25 09:24
以下是解决问题之后的结果

感謝大大回覆~但執行時會發生以下的情況

7*6=
Please input your answer:(q for exit) q        
Traceback (most recent call last):
  File "nice50.py", line 13, in <module>
    question = input('Please input your answer:(q for exit)')
  File "<string>", line 1, in <module>
NameError: name 'q' is not defined
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-25 19:29:24 | 显示全部楼层
game9910 发表于 2021-2-25 19:14
感謝大大回覆,但我執行後會發生
7*3=
Please input your answer:(q for exit)21

改成这样:
  1. import random

  2. count = 0       #答题總數
  3. right = 0       #正確數量

  4. while True:
  5.     a = random.randint(0,9)
  6.     b = random.randint(0,9)
  7.     print('%d*%d=' %(a,b))
  8.     question = input('Please input your answer:(q for exit)')
  9.     if question == 'q':
  10.         break
  11.     result = a * b
  12.     if question.isdigit():
  13.         if int(question) == result:
  14.             print('OK!')
  15.             right += 1
  16.             count += 1
  17.         else:
  18.             print('Failed!')
  19.             count += 1
  20.     else:
  21.         print('Failed!')
  22.         count += 1

  23. percent = right / count
  24. print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-25 20:10:50 | 显示全部楼层

感謝大大回覆,但執行後,還是過不去。

5*7=
Please input your answer:(q for exit)35
Traceback (most recent call last):
  File "nice50.py", line 16, in <module>
    if question.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-25 20:31:37 | 显示全部楼层
game9910 发表于 2021-2-25 20:10
感謝大大回覆,但執行後,還是過不去。

5*7=

蛤?我这里没有问题啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-25 20:39:23 | 显示全部楼层
qiuyouzhi 发表于 2021-2-25 20:31
蛤?我这里没有问题啊

這是我整段的語法,就照你貼的複製上去,但就會產生那個結果也.
真的很奇怪??

#coding:utf-8
import random

count = 0       #答题總數
right = 0       #正確數量

while True:
    a = random.randint(0,9)
    b = random.randint(0,9)
    print('%d*%d=' %(a,b))
    question = input('Please input your answer:(q for exit)')
    if question == 'q':
        break
    result = a * b
    if question.isdigit():
        if int(question) == result:
            print('OK!')
            right += 1
            count += 1
        else:
            print('Failed!')
            count += 1
    else:
        print('Failed!')
        count += 1

percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-26 08:46:49 | 显示全部楼层    本楼为最佳答案   
我运行的都没问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-27 09:56:07 | 显示全部楼层
感謝各位大大的解答,已經找到解法了,
主要是 我的python的版本太低2.7,升級到3.9後就沒問題了~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-7 10:51:45 | 显示全部楼层
game9910 发表于 2021-2-25 19:16
感謝大大回覆~但執行時會發生以下的情況

7*6=
  1. if question != 'q' and int(question) == result:#判断question是否不等于'q'的同时与正确答案相等,question != 'q'为了防止出错
复制代码


这行代码好像就是为了防止出错而写的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-7 10:56:11 | 显示全部楼层
game9910 发表于 2021-2-25 19:16
感謝大大回覆~但執行時會發生以下的情況

7*6=

sorry,缩进漏了
  1. import random

  2. count = 0       #答题總數
  3. right = 0       #正確數量

  4. while True:
  5.     a = random.randint(0,9)
  6.     b = random.randint(0,9)
  7.     print('%d*%d=' %(a,b))
  8.     question = input('Please input your answer:(q for exit)')
  9.     result = a * b
  10.     if question != 'q' and int(question) == result:#判断question是否不等于'q'的同时与正确答案相等,question != 'q'为了防止出错
  11.         print('OK!')
  12.         right += 1
  13.         count += 1
  14.     elif question == 'q':
  15.         break
  16.     else:
  17.         print('Failed!')
  18.         count +=1
  19.         
  20.     percent = right / count#这边漏了一个缩进
  21. print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 09:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表