Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import random
|
| 3 |
|
| 4 |
# ------------------------
|
| 5 |
-
# 🎮
|
| 6 |
# ------------------------
|
| 7 |
|
| 8 |
quiz_questions = [
|
|
@@ -16,79 +16,73 @@ quiz_questions = [
|
|
| 16 |
]
|
| 17 |
|
| 18 |
# ------------------------
|
| 19 |
-
#
|
| 20 |
# ------------------------
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
{"question": "What is 9 - 2?", "options": ["7", "6", "5"], "answer": "7", "hint": "Simple subtraction."},
|
| 26 |
-
{"question": "What is 12 / 3?", "options": ["3", "4", "5"], "answer": "4", "hint": "Division."},
|
| 27 |
-
{"question": "What is 15 % 4?", "options": ["3", "4", "5"], "answer": "3", "hint": "Modulo operation."},
|
| 28 |
-
{"question": "What is 8 * 7?", "options": ["56", "54", "58"], "answer": "56", "hint": "Multiplication."},
|
| 29 |
-
{"question": "What is 5 + 9?", "options": ["14", "13", "12"], "answer": "14", "hint": "Addition."}
|
| 30 |
-
]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
{"question": "What is the use of 'input()'?", "options": ["To take user input", "To print a value", "To define a function"], "answer": "To take user input", "hint": "User interaction."},
|
| 43 |
-
{"question": "What is a tuple?", "options": ["Immutable list", "Mutable list", "String"], "answer": "Immutable list", "hint": "Cannot be changed after creation."}
|
| 44 |
-
]
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
# ------------------------
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
nonlocal score
|
| 61 |
-
if user_answer == answer:
|
| 62 |
-
score += 1
|
| 63 |
-
return score, f"Hint: {hint} - Next Question"
|
| 64 |
-
|
| 65 |
-
return gr.Interface(
|
| 66 |
-
fn=check_answer,
|
| 67 |
-
inputs=[gr.Dropdown(choices=options, label=f"{game_type}: {question_text}")],
|
| 68 |
-
outputs=[gr.Textbox(label="Your Score"), gr.Textbox(label="Hint")],
|
| 69 |
-
live=True
|
| 70 |
)
|
| 71 |
|
| 72 |
# ------------------------
|
| 73 |
-
# GRADIO
|
| 74 |
# ------------------------
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import random
|
| 3 |
|
| 4 |
# ------------------------
|
| 5 |
+
# 🎮 GUESS THE OUTPUT QUIZ
|
| 6 |
# ------------------------
|
| 7 |
|
| 8 |
quiz_questions = [
|
|
|
|
| 16 |
]
|
| 17 |
|
| 18 |
# ------------------------
|
| 19 |
+
# GAME LOGIC
|
| 20 |
# ------------------------
|
| 21 |
|
| 22 |
+
def get_new_question():
|
| 23 |
+
q = random.choice(quiz_questions)
|
| 24 |
+
return q
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def start_game():
|
| 27 |
+
question = get_new_question()
|
| 28 |
+
return (
|
| 29 |
+
question["code"],
|
| 30 |
+
gr.update(choices=question["options"], value=None),
|
| 31 |
+
"", "", # Clear result and hint
|
| 32 |
+
question, # Save question in state
|
| 33 |
+
0 # Initial score
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
def submit_answer(user_answer, current_question, score):
|
| 37 |
+
if user_answer == current_question["answer"]:
|
| 38 |
+
score += 1
|
| 39 |
+
result = f"✅ Correct! Your score is now {score}"
|
| 40 |
+
else:
|
| 41 |
+
result = f"❌ Wrong! The correct answer was '{current_question['answer']}'"
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Show hint
|
| 44 |
+
hint = f"💡 Hint: {current_question['hint']}"
|
|
|
|
| 45 |
|
| 46 |
+
# Load next question
|
| 47 |
+
next_question = get_new_question()
|
| 48 |
+
|
| 49 |
+
return (
|
| 50 |
+
next_question["code"],
|
| 51 |
+
gr.update(choices=next_question["options"], value=None),
|
| 52 |
+
result,
|
| 53 |
+
hint,
|
| 54 |
+
next_question,
|
| 55 |
+
score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
# ------------------------
|
| 59 |
+
# GRADIO UI
|
| 60 |
# ------------------------
|
| 61 |
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("## 🧠 Learn Python Through Games - Guess the Output Edition")
|
| 64 |
+
|
| 65 |
+
start_btn = gr.Button("🎲 Start Quiz")
|
| 66 |
+
|
| 67 |
+
question_display = gr.Textbox(label="🧩 Code Snippet", interactive=False)
|
| 68 |
+
options_dropdown = gr.Dropdown(choices=[], label="Choose the Output")
|
| 69 |
+
submit_btn = gr.Button("Submit Answer")
|
| 70 |
+
|
| 71 |
+
result_text = gr.Textbox(label="Result", interactive=False)
|
| 72 |
+
hint_text = gr.Textbox(label="Hint", interactive=False)
|
| 73 |
+
|
| 74 |
+
# Hidden states to store session data
|
| 75 |
+
question_state = gr.State()
|
| 76 |
+
score_state = gr.State()
|
| 77 |
+
|
| 78 |
+
# Link start button to load first question
|
| 79 |
+
start_btn.click(start_game,
|
| 80 |
+
inputs=[],
|
| 81 |
+
outputs=[question_display, options_dropdown, result_text, hint_text, question_state, score_state])
|
| 82 |
+
|
| 83 |
+
# Link submit button to check answer and load next
|
| 84 |
+
submit_btn.click(submit_answer,
|
| 85 |
+
inputs=[options_dropdown, question_state, score_state],
|
| 86 |
+
outputs=[question_display, options_dropdown, result_text, hint_text, question_state, score_state])
|
| 87 |
+
|
| 88 |
+
demo.launch()
|