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

# 🧠 Sample questions for kids
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", "52", "2"],
        "answer": "7",
        "hint": "What is 5 + 2?"
    }
]

# 🔁 Track state
score = 0
question_index = 0
history = []

def get_current_question():
    return questions[question_index]

def show_question():
    q = get_current_question()
    return q["code"], gr.update(choices=q["options"]), "", f"⭐ Score: {score}"

def check_answer(choice):
    global score
    correct = get_current_question()["answer"]
    result = ""
    if choice == correct:
        result = "✅ Correct! Great job!"
        score_msg = f"⭐ Score: {score + 1}"
        score += 1
    else:
        result = f"❌ Oops! The correct answer was: {correct}"
        score_msg = f"⭐ Score: {score}"
    return result, score_msg

def skip_question():
    next_q()
    return show_question()

def get_hint():
    return f"💡 Hint: {get_current_question()['hint']}"

def next_q():
    global question_index
    history.append(question_index)
    question_index = (question_index + 1) % len(questions)

def prev_q():
    global question_index
    if history:
        question_index = history.pop()
    return