10以内加减法,能根据输入题数出题,判断做题是否正确,最后计算分数。python
时间: 2023-10-08 11:11:34 浏览: 171
基于python实现混合加减法出题程序项目源码分享
以下一个简单的实现,可以根据输入的题数出相应数量的加减法题目,然后让用户输入答案并判断是否正确,最后计算分数。注意,这个实现只考虑了整数的加减法,并且没有进行错误输入的处理。
```python
import random
# 生成加减法题目
def generate_questions(num):
questions = []
for i in range(num):
a = random.randint(0, 10)
b = random.randint(0, 10)
op = random.choice(['+', '-'])
question = f"{a} {op} {b} = "
questions.append(question)
return questions
# 判断用户输入的答案是否正确
def check_answer(question, answer):
result = eval(question[:-2])
return result == int(answer)
# 计算分数
def calculate_score(correct_count, total_count):
return correct_count / total_count * 100
# 主程序
if __name__ == '__main__':
num_questions = int(input("请输入题目数量:"))
questions = generate_questions(num_questions)
correct_count = 0
for question in questions:
user_answer = input(question)
if check_answer(question, user_answer):
print("回答正确!")
correct_count += 1
else:
print("回答错误!")
score = calculate_score(correct_count, num_questions)
print(f"你的得分是:{score:.2f} 分")
```
运行结果示例:
```
请输入题目数量:3
6 + 7 = 13
回答正确!
7 - 4 = 3
回答正确!
4 + 0 = 5
回答错误!
你的得分是:66.67 分
```
阅读全文