3、随机生成10题20以内的加减法算术题,如果每题回答正确计分10分,并给出最后的得分Python。
时间: 2024-09-28 20:06:56 浏览: 28
10以内加减法练习题.doc
在Python中,你可以使用random模块来生成随机数,以及if语句来检查用户的答案是否正确。这里是一个简单的示例代码,用于生成10道20以内的加减法题目,并根据用户输入计算得分:
```python
import random
def generate_question():
operators = ['+', '-']
num1 = random.randint(1, 20)
num2 = random.randint(1, 20)
question = f"{num1} {operators[random.randint(0, 1)]} {num2}"
return question, num1 + num2 if operators[random.randint(0, 1)] == '+' else num1 - num2
def check_answer(user_input, correct_answer):
user_score = 0
if user_input == correct_answer:
print(f"恭喜,答对了!得分为10分")
user_score += 10
else:
print(f"很遗憾,答案错误。正确答案是{correct_answer}")
return user_score
total_score = 0
for _ in range(10):
question, correct_answer = generate_question()
print(question)
user_input = int(input("请输入你的答案: "))
total_score += check_answer(user_input, correct_answer)
print(f"最终得分: {total_score} 分")
阅读全文