Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import random | |
| def generate_question(): | |
| a = random.randint(1, 20) | |
| b = random.randint(1, 20) | |
| op = random.choice(["+", "-", "*"]) | |
| question = f"{a} {op} {b}" | |
| answer = eval(question) | |
| return question, answer | |
| current_q, current_ans = generate_question() | |
| def new_question(): | |
| global current_q, current_ans | |
| current_q, current_ans = generate_question() | |
| return current_q, "" | |
| def check_answer(user_input): | |
| try: | |
| if int(user_input) == current_ans: | |
| msg = "🎯 Correct!" | |
| q, _ = new_question() | |
| return q, msg | |
| else: | |
| return current_q, "❌ Wrong, try again!" | |
| except: | |
| return current_q, "⚠️ Please enter a number." | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ➗ Math Quiz Game") | |
| question_box = gr.Textbox(value=current_q, interactive=False, label="Question") | |
| answer_input = gr.Textbox(label="Your Answer") | |
| result = gr.Textbox(interactive=False, label="Result") | |
| submit_btn = gr.Button("Submit Answer") | |
| new_btn = gr.Button("New Question") | |
| submit_btn.click(fn=check_answer, inputs=answer_input, outputs=[question_box, result]) | |
| new_btn.click(fn=new_question, inputs=[], outputs=[question_box, result]) | |
| demo.launch() | |