|
本帖最后由 game9910 于 2021-2-20 15:30 编辑
想求助 各位大大,最近想利用 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))
|
|