數學乘法執行問題-一直無法驗算結果
想求助 各位大大,最近想利用 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))
改成这样:
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() and int(question) == result:
print('OK!')
right += 1
count += 1
else:
print('Failed!')
count += 1
percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100)) 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 == 'q':
break
elif int(question) == int(result):
print('OK!')
right += 1
count += 1
else:
print('Failed!')
count +=1
percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
3*0=
Please input your answer:(q for exit)0
OK!
3*0=
Please input your answer:(q for exit)0
OK!
9*5=
Please input your answer:(q for exit)45
OK!
3*9=
Please input your answer:(q for exit)20
Failed!
6*4=
Please input your answer:(q for exit)q
测试结束,共回答4道题,正确个数为3,正确率为75.00%
#原代码
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))
以下是解决问题之后的结果
#改编后的代码
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 != 'q' and int(question) == result:#判断question是否不等于'q'的同时与正确答案相等,question != 'q'为了防止出错
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-24 21:00
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要跳出時會發生這個錯誤??
qiuyouzhi 发表于 2021-2-24 20:56
改成这样:
感謝大大回覆,但我執行後會發生
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'
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 game9910 发表于 2021-2-25 19:14
感謝大大回覆,但我執行後會發生
7*3=
Please input your answer:(q for exit)21
改成这样:
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)) qiuyouzhi 发表于 2021-2-25 19:29
改成这样:
感謝大大回覆,但執行後,還是過不去。{:5_100:}
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' game9910 发表于 2021-2-25 20:10
感謝大大回覆,但執行後,還是過不去。
5*7=
蛤?我这里没有问题啊
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))
我运行的都没问题 感謝各位大大的解答,已經找到解法了,
主要是 我的python的版本太低2.7,升級到3.9後就沒問題了~{:10_256:} game9910 发表于 2021-2-25 19:16
感謝大大回覆~但執行時會發生以下的情況
7*6=
if question != 'q' and int(question) == result:#判断question是否不等于'q'的同时与正确答案相等,question != 'q'为了防止出错
这行代码好像就是为了防止出错而写的 game9910 发表于 2021-2-25 19:16
感謝大大回覆~但執行時會發生以下的情況
7*6=
sorry,缩进漏了
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 != 'q' and int(question) == result:#判断question是否不等于'q'的同时与正确答案相等,question != 'q'为了防止出错
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))
页:
[1]