1 Star 0 Fork 2

武斌/SmartEdu_1

forked from Akai_akari/SmartEdu 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mathQuestions.py 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/guai128/smart-edu_1.git
[email protected]:guai128/smart-edu_1.git
guai128
smart-edu_1
SmartEdu_1
master

搜索帮助