File size: 3,241 Bytes
8e6f243
5bbe733
8e6f243
96f5111
c87a8cd
96f5111
 
 
47de747
 
 
 
 
 
 
5bbe733
 
96f5111
c87a8cd
96f5111
 
c87a8cd
 
 
96f5111
c87a8cd
 
 
 
 
 
 
 
 
96f5111
c87a8cd
 
 
 
 
 
96f5111
c87a8cd
 
47de747
c87a8cd
 
 
 
 
 
 
 
 
 
47de747
 
 
c87a8cd
47de747
 
c87a8cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import random

# ------------------------
# 🎮 GUESS THE OUTPUT QUIZ
# ------------------------

quiz_questions = [
    {"code": 'print("Hello" + "World")', "options": ["HelloWorld", "Hello World", "Hello+World"], "answer": "HelloWorld", "hint": "What happens when you add two strings?"},
    {"code": 'print(2 * 3)', "options": ["6", "23", "5"], "answer": "6", "hint": "Multiplication or joining?"},
    {"code": 'x = 5\nprint(x + 2)', "options": ["7", "10", "5"], "answer": "7", "hint": "Adding a number to a variable."},
    {"code": 'print(3 + 5 * 2)', "options": ["13", "16", "8"], "answer": "13", "hint": "Order of operations."},
    {"code": 'print("Python"[0])', "options": ["P", "Y", "p"], "answer": "P", "hint": "What does the index 0 in a string return?"},
    {"code": 'x = [1, 2, 3]\nprint(x[1])', "options": ["2", "3", "1"], "answer": "2", "hint": "Indexing a list."},
    {"code": 'print("5" + "5")', "options": ["10", "55", "50"], "answer": "55", "hint": "What happens when you add strings?"}
]

# ------------------------
# GAME LOGIC
# ------------------------

def get_new_question():
    q = random.choice(quiz_questions)
    return q

def start_game():
    question = get_new_question()
    return (
        question["code"], 
        gr.update(choices=question["options"], value=None),
        "", "",  # Clear result and hint
        question,  # Save question in state
        0          # Initial score
    )

def submit_answer(user_answer, current_question, score):
    if user_answer == current_question["answer"]:
        score += 1
        result = f"✅ Correct! Your score is now {score}"
    else:
        result = f"❌ Wrong! The correct answer was '{current_question['answer']}'"

    # Show hint
    hint = f"💡 Hint: {current_question['hint']}"

    # Load next question
    next_question = get_new_question()

    return (
        next_question["code"],
        gr.update(choices=next_question["options"], value=None),
        result,
        hint,
        next_question,
        score
    )

# ------------------------
# GRADIO UI
# ------------------------

with gr.Blocks() as demo:
    gr.Markdown("## 🧠 Learn Python Through Games - Guess the Output Edition")

    start_btn = gr.Button("🎲 Start Quiz")

    question_display = gr.Textbox(label="🧩 Code Snippet", interactive=False)
    options_dropdown = gr.Dropdown(choices=[], label="Choose the Output")
    submit_btn = gr.Button("Submit Answer")

    result_text = gr.Textbox(label="Result", interactive=False)
    hint_text = gr.Textbox(label="Hint", interactive=False)

    # Hidden states to store session data
    question_state = gr.State()
    score_state = gr.State()

    # Link start button to load first question
    start_btn.click(start_game, 
                    inputs=[], 
                    outputs=[question_display, options_dropdown, result_text, hint_text, question_state, score_state])

    # Link submit button to check answer and load next
    submit_btn.click(submit_answer, 
                     inputs=[options_dropdown, question_state, score_state], 
                     outputs=[question_display, options_dropdown, result_text, hint_text, question_state, score_state])

demo.launch()