|
发表于 2021-2-25 09:24:32
|
显示全部楼层
- #原代码
- 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))
复制代码
|
|