import random
import time

score = 0
start_time = time.time()

while time.time() - start_time < 30:
    a = random.randint(0, 10)
    b = random.randint(1, 10)
    op = random.choice(['+', '-', '*', '/'])
    if op == '+':
        ans = a + b
    elif op == '-':
        ans = a - b
    elif op == '*':
        ans = a * b
    else:
        ans = a // b
        a = ans * b  # 保证除法能够除尽
    print(f"What is {a} {op} {b}?")
    try:
        user_ans = int(input())
    except ValueError:
        print("Invalid input. Please enter an integer.")
        continue
    if user_ans == ans:
        score += 1
    else:
        print(f"Wrong. The answer is {ans}.")
        start_time += 2  # 错误扣2秒

print(f"Time's up! Your score is {score}.")
``
用Python3写一段做计算题的代码生成一个0到10的随机数有加减乘除除法必须能够除尽随机出题并且每题只有一次答题机会每题一分所有答题时间限时30秒错误扣2秒正确不加时间继续下一题直至时间用完

原文地址: http://www.cveoy.top/t/topic/huF3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录