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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
def generate_problem(operation, min_value, max_value):
|
| 5 |
+
"""
|
| 6 |
+
Generates a random math problem based on the selected operation and range.
|
| 7 |
+
"""
|
| 8 |
+
# Ensure min_value is less than or equal to max_value
|
| 9 |
+
if min_value > max_value:
|
| 10 |
+
min_value, max_value = max_value, min_value
|
| 11 |
+
|
| 12 |
+
num1 = random.randint(int(min_value), int(max_value))
|
| 13 |
+
num2 = random.randint(int(min_value), int(max_value))
|
| 14 |
+
# Avoid division by zero
|
| 15 |
+
if operation == 'Divide':
|
| 16 |
+
while num2 == 0:
|
| 17 |
+
num2 = random.randint(int(min_value), int(max_value))
|
| 18 |
+
return num1, num2
|
| 19 |
+
|
| 20 |
+
def update_question(operation, min_value, max_value):
|
| 21 |
+
"""
|
| 22 |
+
Updates the question text and generates new random numbers.
|
| 23 |
+
"""
|
| 24 |
+
num1, num2 = generate_problem(operation, min_value, max_value)
|
| 25 |
+
op_symbol = {'Add': '+', 'Subtract': '-', 'Multiply': '*', 'Divide': '/'}[operation]
|
| 26 |
+
question = f"What is {num1} {op_symbol} {num2}?"
|
| 27 |
+
return question, num1, num2
|
| 28 |
+
|
| 29 |
+
def check_answer(operation, user_answer, num1, num2, min_value, max_value, correct_tally, incorrect_tally):
|
| 30 |
+
"""
|
| 31 |
+
Checks the user's answer, updates tallies, and returns the result message and a new question.
|
| 32 |
+
"""
|
| 33 |
+
# Calculate the correct answer
|
| 34 |
+
if operation == 'Add':
|
| 35 |
+
correct_answer = num1 + num2
|
| 36 |
+
elif operation == 'Subtract':
|
| 37 |
+
correct_answer = num1 - num2
|
| 38 |
+
elif operation == 'Multiply':
|
| 39 |
+
correct_answer = num1 * num2
|
| 40 |
+
elif operation == 'Divide':
|
| 41 |
+
correct_answer = num1 / num2
|
| 42 |
+
|
| 43 |
+
# Check if the user's answer is correct
|
| 44 |
+
if user_answer == correct_answer:
|
| 45 |
+
result = "Correct!"
|
| 46 |
+
correct_tally += 1
|
| 47 |
+
else:
|
| 48 |
+
result = f"Incorrect. The correct answer is {correct_answer}."
|
| 49 |
+
incorrect_tally += 1
|
| 50 |
+
|
| 51 |
+
# Generate a new question for the next round
|
| 52 |
+
question_new, num1_new, num2_new = update_question(operation, min_value, max_value)
|
| 53 |
+
|
| 54 |
+
# Prepare the tally message
|
| 55 |
+
tally_message = f"Correct Answers: {correct_tally}\nIncorrect Answers: {incorrect_tally}"
|
| 56 |
+
|
| 57 |
+
# Return all updated values
|
| 58 |
+
return result, question_new, num1_new, num2_new, correct_tally, incorrect_tally, tally_message
|
| 59 |
+
|
| 60 |
+
def reset_tallies(correct_tally, incorrect_tally):
|
| 61 |
+
"""
|
| 62 |
+
Resets the correct and incorrect tallies to zero and updates the tally display.
|
| 63 |
+
"""
|
| 64 |
+
correct_tally = 0
|
| 65 |
+
incorrect_tally = 0
|
| 66 |
+
tally_message = f"Correct Answers: {correct_tally}\nIncorrect Answers: {incorrect_tally}"
|
| 67 |
+
return correct_tally, incorrect_tally, tally_message
|
| 68 |
+
|
| 69 |
+
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## Math Quiz\nSelect an operation, set the range, solve the problem, and enter your answer.")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
# Operation selection
|
| 74 |
+
operation = gr.Radio(['Add', 'Subtract', 'Multiply', 'Divide'], label='Select Operation', value='Add')
|
| 75 |
+
|
| 76 |
+
# Range inputs
|
| 77 |
+
min_value = gr.Number(label='Minimum Value', value=-100)
|
| 78 |
+
max_value = gr.Number(label='Maximum Value', value=100)
|
| 79 |
+
|
| 80 |
+
# Question display
|
| 81 |
+
question_text = gr.Textbox(label='Question', interactive=False)
|
| 82 |
+
|
| 83 |
+
# User's answer input
|
| 84 |
+
answer = gr.Number(label='Your Answer')
|
| 85 |
+
|
| 86 |
+
# Result display
|
| 87 |
+
result = gr.Textbox(label='Result', interactive=False)
|
| 88 |
+
|
| 89 |
+
# Tally display
|
| 90 |
+
tally_display = gr.Textbox(label='Tally', value="Correct Answers: 0\nIncorrect Answers: 0", interactive=False)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
# Button to check the answer
|
| 94 |
+
check_button = gr.Button('Check Answer')
|
| 95 |
+
|
| 96 |
+
# Button to reset tallies
|
| 97 |
+
reset_button = gr.Button('Reset Tallies')
|
| 98 |
+
|
| 99 |
+
# Hidden states to store the current numbers and tallies
|
| 100 |
+
num1_state = gr.State()
|
| 101 |
+
num2_state = gr.State()
|
| 102 |
+
correct_tally = gr.State(0)
|
| 103 |
+
incorrect_tally = gr.State(0)
|
| 104 |
+
|
| 105 |
+
# Initialize the interface with the first question and tallies
|
| 106 |
+
def init_interface():
|
| 107 |
+
question, num1, num2 = update_question(operation.value, min_value.value, max_value.value)
|
| 108 |
+
tally_message = f"Correct Answers: {correct_tally.value}\nIncorrect Answers: {incorrect_tally.value}"
|
| 109 |
+
return question, num1, num2, tally_message
|
| 110 |
+
|
| 111 |
+
# Set initial values for the question, numbers, and tally
|
| 112 |
+
init_question, init_num1, init_num2, init_tally_message = init_interface()
|
| 113 |
+
question_text.value = init_question
|
| 114 |
+
num1_state.value = init_num1
|
| 115 |
+
num2_state.value = init_num2
|
| 116 |
+
tally_display.value = init_tally_message
|
| 117 |
+
|
| 118 |
+
# Update the question when the operation or range changes
|
| 119 |
+
def on_operation_or_range_change(op, min_val, max_val, correct_tally, incorrect_tally):
|
| 120 |
+
question, num1, num2 = update_question(op, min_val, max_val)
|
| 121 |
+
tally_message = f"Correct Answers: {correct_tally}\nIncorrect Answers: {incorrect_tally}"
|
| 122 |
+
return question, num1, num2, tally_message
|
| 123 |
+
|
| 124 |
+
operation.change(
|
| 125 |
+
fn=on_operation_or_range_change,
|
| 126 |
+
inputs=[operation, min_value, max_value, correct_tally, incorrect_tally],
|
| 127 |
+
outputs=[question_text, num1_state, num2_state, tally_display]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
min_value.change(
|
| 131 |
+
fn=on_operation_or_range_change,
|
| 132 |
+
inputs=[operation, min_value, max_value, correct_tally, incorrect_tally],
|
| 133 |
+
outputs=[question_text, num1_state, num2_state, tally_display]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
max_value.change(
|
| 137 |
+
fn=on_operation_or_range_change,
|
| 138 |
+
inputs=[operation, min_value, max_value, correct_tally, incorrect_tally],
|
| 139 |
+
outputs=[question_text, num1_state, num2_state, tally_display]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Check the user's answer when the button is clicked
|
| 143 |
+
def on_check_answer(operation, user_answer, num1, num2, min_value, max_value, correct_tally, incorrect_tally):
|
| 144 |
+
result_text, question_new, num1_new, num2_new, correct_tally_new, incorrect_tally_new, tally_message = check_answer(
|
| 145 |
+
operation, user_answer, num1, num2, min_value, max_value, correct_tally, incorrect_tally)
|
| 146 |
+
return result_text, question_new, num1_new, num2_new, correct_tally_new, incorrect_tally_new, tally_message
|
| 147 |
+
|
| 148 |
+
check_button.click(
|
| 149 |
+
fn=on_check_answer,
|
| 150 |
+
inputs=[operation, answer, num1_state, num2_state, min_value, max_value, correct_tally, incorrect_tally],
|
| 151 |
+
outputs=[result, question_text, num1_state, num2_state, correct_tally, incorrect_tally, tally_display]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Reset the tallies when the reset button is clicked
|
| 155 |
+
def on_reset_tallies(correct_tally, incorrect_tally):
|
| 156 |
+
correct_tally_new, incorrect_tally_new, tally_message = reset_tallies(correct_tally, incorrect_tally)
|
| 157 |
+
return correct_tally_new, incorrect_tally_new, tally_message
|
| 158 |
+
|
| 159 |
+
reset_button.click(
|
| 160 |
+
fn=on_reset_tallies,
|
| 161 |
+
inputs=[correct_tally, incorrect_tally],
|
| 162 |
+
outputs=[correct_tally, incorrect_tally, tally_display]
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio==5.1.0
|