请编写一段PYTHON代码,功能要求:随机生成两个两位数及加减号(要保证减法算式的被减数大于减数),显示题目让学生输入计算结果,判断并显示结果的正确与否(或给予累积得分)。可考虑设置答题结束条件为时间限制(如1分钟内完成)或题目总数限制(如20题),也可以是其它条件(自行设置)。
时间: 2023-05-31 08:06:09 浏览: 362
import random
import time
score = 0
total_questions = 20
questions_answered = 0
start_time = time.time()
while questions_answered < total_questions and time.time() - start_time < 60:
num1 = random.randint(10, 99)
num2 = random.randint(10, 99)
operator = random.choice(['+', '-'])
if operator == '+':
answer = num1 + num2
question = f"{num1} + {num2} = "
else:
answer = num1 - num2
question = f"{num1} - {num2} = "
user_answer = input(question)
try:
user_answer = int(user_answer)
except ValueError:
user_answer = None
if user_answer == answer:
print("Correct!")
score += 1
else:
print("Incorrect.")
questions_answered += 1
print(f"You answered {score} out of {total_questions} questions correctly in {time.time() - start_time:.2f} seconds.")
阅读全文