File size: 1,661 Bytes
8e6f243
5bbe733
8e6f243
5bbe733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e6f243
5bbe733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e6f243
5bbe733
 
8e6f243
5bbe733
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
import gradio as gr
import random

# Sample Python code questions
questions = [
    {
        "code": 'print("Hello" + "World")',
        "options": ["HelloWorld", "Hello World", "Hello+World"],
        "answer": "HelloWorld"
    },
    {
        "code": 'print(2 * 3)',
        "options": ["6", "23", "5"],
        "answer": "6"
    },
    {
        "code": 'x = 5\nprint(x + 2)',
        "options": ["7", "52", "2"],
        "answer": "7"
    }
]

score = 0
current_q = random.choice(questions)

def get_question():
    global current_q
    current_q = random.choice(questions)
    return current_q["code"], current_q["options"]

def check_answer(choice):
    global score
    if choice == current_q["answer"]:
        score += 1
        return f"โœ… Correct! ๐ŸŽ‰ Your Score: {score}"
    else:
        return f"โŒ Oops! Try again. Correct answer was: {current_q['answer']}"

# Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as quiz:
    gr.Markdown("## ๐Ÿง  Python Quiz Game for Kids ๐ŸŽฎ")
    code_display = gr.Textbox(label="๐Ÿ‘‡ What will this Python code print?", lines=2, interactive=False)
    options = gr.Radio(choices=[], label="Choose your answer")
    result = gr.Textbox(label="Your Result")
    btn_check = gr.Button("Check Answer")
    btn_next = gr.Button("Next Question")

    def show_question():
        code, opts = get_question()
        return code, gr.update(choices=opts)

    btn_check.click(fn=check_answer, inputs=options, outputs=result)
    btn_next.click(fn=show_question, outputs=[code_display, options])

    # Load the first question on launch
    quiz.load(show_question, outputs=[code_display, options])

quiz.launch()