代码拉取完成,页面将自动刷新
同步操作将从 Akai_akari/SmartEdu 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import random
import matplotlib.pyplot as plt
def generate_question(max_num, operators):
a = random.randint(1, max_num)
b = random.randint(1, max_num)
op = random.choice(operators)
question = f"{a} {op} {b} = ___"
if op == "+":
answer = a + b
elif op == "-":
answer = a - b
elif op == "*":
answer = a * b
elif op == "/":
while b == 0:
b = random.randint(1, max_num)
answer = round(a / b, 2)
else:
raise ValueError("Invalid operator!")
return question, answer
def generate_questions(num, max_num, operators):
questions = []
answers = []
for _ in range(num):
question, answer = generate_question(max_num, operators)
questions.append(question)
answers.append(answer)
return questions, answers
def ask_and_check(questions, answers):
score = 0
for i in range(len(questions)):
print(questions[i])
print("\n请选择操作:\n1. 直接回答问题\n2. 将问题打印成图片")
choice = input("请选择操作 (1 或 2): ")
if choice == "1":
print("\n请输入答案(小数答案四舍五入保留两位):")
for i in range(len(questions)):
user_answer_input = input(f"Question {i+1}: ")
if not user_answer_input:
print(f"Incorrect. The correct answer is {answers[i]}\n")
continue
user_answer = float(user_answer_input)
rounded_user_answer = round(user_answer, 2)
if rounded_user_answer == round(answers[i], 2):
print("Correct!\n")
score += 1
else:
print(f"Incorrect. The correct answer is {answers[i]}\n")
elif choice == "2":
fig, ax = plt.subplots()
ax.axis('off')
ax.text(0.5, 0.5, '\n'.join(questions), size=12, va='center', ha='center')
plt.show()
else:
print("无效选择")
print(f"You got {score} out of {len(questions)} questions correct.")
def run_quiz():
print('请输入最大数字范围(例如,100):')
max_num = int(input())
print('请选择运算符,使用空格分隔(+ - * /):')
operators = input().split()
questions, answers = generate_questions(10, max_num, operators)
ask_and_check(questions, answers)
if __name__ == "__main__":
run_quiz()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。