试编写一个程序来帮助小学生学习四则运算。参考本章介绍的猜数游戏(例6.7、例6.11和例6.12)程序,按下列任务要求以循序渐进的方式编程。 任务1:程序首先显示菜单,选择做加减乘除哪一种运算。根据用户选择的运算类型,随机产生两个1~10之间的正整数,在屏幕上打印出问题,例如: 6 * 7 = 然后让学生输入答案。程序检查学生输入的答案是否正确。若学生回答正确,则显示“Right!”,否则显示“Wrong!”。 任务2:10道题全部做完后,按每题10分统计并输出总分,同时为了记录学生能力提高的过程,再输出学生的回答正确率,根据准确率输出不同的评价。 任务3:在任务2的基础上,为使学生通过反复练习熟练掌握所学内容,在学生完成10道运算题后,若回答正确率低于75%,则重新做10道题,直到回答正确率高于75%时才退出程序。 任务4:错题本功能记录做错的题目,选择该选项可以重做之前做过的错题。(最多记录20道)。
时间: 2023-06-13 17:07:48 浏览: 112
一个简单的猜数游戏的Python程序.pdf
以下是一个简单的实现:
```python
import random
class Question:
def __init__(self, num1, num2, op):
self.num1 = num1
self.num2 = num2
self.op = op
self.answer = self.calculate()
def calculate(self):
if self.op == '+':
return self.num1 + self.num2
elif self.op == '-':
return self.num1 - self.num2
elif self.op == '*':
return self.num1 * self.num2
elif self.op == '/':
return self.num1 // self.num2
class Quiz:
def __init__(self):
self.score = 0
self.total_questions = 0
self.correct_answers = 0
self.wrong_answers = 0
self.wrong_questions = []
self.max_wrong_questions = 20
def reset(self):
self.score = 0
self.total_questions = 0
self.correct_answers = 0
self.wrong_answers = 0
self.wrong_questions = []
def generate_question(self):
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
op = random.choice(['+', '-', '*', '/'])
return Question(num1, num2, op)
def ask_question(self):
question = self.generate_question()
answer = input(f'{question.num1} {question.op} {question.num2} = ')
try:
answer = int(answer)
except ValueError:
print('Invalid input, please enter an integer.')
return self.ask_question()
if answer == question.answer:
print('Right!')
self.score += 10
self.correct_answers += 1
else:
print('Wrong!')
self.score -= 5
self.wrong_answers += 1
if len(self.wrong_questions) < self.max_wrong_questions:
self.wrong_questions.append(question)
def run(self):
print('Welcome to the Math Quiz!')
while True:
print('Please select the operation type:')
print('1. Addition')
print('2. Subtraction')
print('3. Multiplication')
print('4. Division')
print('0. Exit')
choice = input()
if choice == '0':
break
elif choice not in ['1', '2', '3', '4']:
print('Invalid choice, please try again.')
continue
op = ['+', '-', '*', '/'][int(choice)-1]
self.reset()
while True:
self.total_questions += 1
self.ask_question()
if self.total_questions == 10:
break
accuracy = self.correct_answers / self.total_questions
print(f'You got {self.score} points in total.')
print(f'Accuracy: {accuracy:.2%}')
if accuracy < 0.75:
print('You need to practice more!')
while True:
self.total_questions += 1
self.ask_question()
if self.total_questions == 10:
break
if self.correct_answers / self.total_questions >= 0.75:
break
if len(self.wrong_questions) > 0:
print(f'You got {len(self.wrong_questions)} questions wrong.')
print('Do you want to review your wrong answers? (y/n)')
choice = input()
if choice.lower() == 'y':
for question in self.wrong_questions:
print(f'{question.num1} {question.op} {question.num2} = {question.answer}')
quiz = Quiz()
quiz.run()
```
这个程序使用了类来组织问题和测试。 Quiz 类负责管理测试的状态和逻辑,如生成问题、检查答案、计算得分和统计正确和错误答案的数量。程序首先显示菜单,让用户选择运算类型,然后开始测试,直到用户退出或达到正确率要求为止。如果用户回答错误率低于 75%,程序会要求用户重复 10 个问题,直到正确率达到要求为止。如果用户回答错误,则记录错误问题,以便稍后查看并修复。
阅读全文