File size: 9,183 Bytes
8e6f243
5bbe733
8e6f243
96f5111
 
 
 
 
5bbe733
 
 
1ba70c1
 
5bbe733
 
 
 
1ba70c1
 
5bbe733
 
 
 
1ba70c1
 
cb21542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bbe733
 
 
96f5111
 
 
5bbe733
96f5111
 
1ba70c1
96f5111
 
 
5bbe733
96f5111
 
 
1ba70c1
 
 
96f5111
8e6f243
1ba70c1
96f5111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb21542
 
 
 
 
 
 
 
 
96f5111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb21542
 
 
 
96f5111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb21542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f5111
 
 
 
 
cb21542
96f5111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c176284
cb21542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import gradio as gr
import random

# ------------------------
# ๐ŸŽฎ 1. 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", "52", "2"],
        "answer": "7",
        "hint": "What is 5 + 2?"
    },
    {
        "code": 'print(10 / 2)',
        "options": ["5", "2", "20"],
        "answer": "5",
        "hint": "What is 10 divided by 2?"
    },
    {
        "code": 'x = "Python"\nprint(x[0])',
        "options": ["P", "y", "t"],
        "answer": "P",
        "hint": "What is the first letter of 'Python'?"
    },
    {
        "code": 'x = [1, 2, 3]\nprint(x[1])',
        "options": ["2", "3", "1"],
        "answer": "2",
        "hint": "Which number is at the second position in the list?"
    },
    {
        "code": 'x = "Code"\nprint(x[::-1])',
        "options": ["edoC", "Code", "CdeO"],
        "answer": "edoC",
        "hint": "What does slicing the string backwards give?"
    }
]

quiz_score = 0
quiz_index = 0
quiz_history = []

def get_quiz_question():
    return quiz_questions[quiz_index]

def show_quiz_question():
    q = get_quiz_question()
    return q["code"], gr.update(choices=q["options"]), "", f"โญ Score: {quiz_score}"

def check_quiz_answer(choice):
    global quiz_score
    correct = get_quiz_question()["answer"]
    result = ""
    if choice == correct:
        result = "โœ… Correct! Great job!"
        quiz_score += 1
    else:
        result = f"โŒ Oops! The correct answer was: {correct}"
    return result, f"โญ Score: {quiz_score}"

def quiz_skip():
    next_quiz()
    return show_quiz_question()

def quiz_hint():
    return f"๐Ÿ’ก Hint: {get_quiz_question()['hint']}"

def next_quiz():
    global quiz_index
    quiz_history.append(quiz_index)
    quiz_index = (quiz_index + 1) % len(quiz_questions)

def prev_quiz():
    global quiz_index
    if quiz_history:
        quiz_index = quiz_history.pop()
    return show_quiz_question()

# ------------------------
# ๐Ÿ”ค 2. WORD SCRAMBLE GAME
# ------------------------

scramble_words = [
    ("python", "A popular programming language"),
    ("loop", "Used to repeat code"),
    ("print", "Outputs something on screen"),
    ("variable", "Stores data values"),
    ("function", "Encapsulates code into reusable blocks"),
    ("list", "A collection of items"),
    ("string", "A sequence of characters")
]

def scramble_word(word):
    return ''.join(random.sample(word, len(word)))

current_scramble = random.choice(scramble_words)

def show_scramble():
    global current_scramble
    current_scramble = random.choice(scramble_words)
    return scramble_word(current_scramble[0]), ""

def check_scramble_answer(ans):
    correct = current_scramble[0]
    if ans.lower() == correct:
        return "โœ… Awesome! That's correct!"
    else:
        return f"โŒ Nope! The right word was '{correct}'."

# ------------------------
# ๐ŸŽฒ 3. CODE DICE
# ------------------------

dice_challenges = [
    "๐Ÿ’ก Create a variable and assign it your name",
    "๐Ÿ’ก Write code to add 2 + 3 and print it",
    "๐Ÿ’ก Use a loop to print numbers from 1 to 5",
    "๐Ÿ’ก Print your favorite color 3 times",
    "๐Ÿ’ก Write a program to check if a number is even or odd",
    "๐Ÿ’ก Create a list and add 3 items to it",
    "๐Ÿ’ก Write a function that takes two numbers and returns their sum"
]

def roll_dice():
    return random.choice(dice_challenges)

# ------------------------
# ๐Ÿงฉ 4. PUZZLE BLOCKS (Ordering)
# ------------------------

puzzle_code = [
    "x = 5",
    "y = 3",
    "z = x + y",
    "print(z)"
]

correct_order = "\n".join(puzzle_code)

def check_code_order(user_code):
    if user_code.strip() == correct_order:
        return "โœ… Perfect order! You built it right!"
    else:
        return "โŒ Hmm... Try again! The order seems off."

# ------------------------
# ๐Ÿ“š 5. CODE FAIRY TALE
# ------------------------

fairy_code = 'print("Cinderella" * 2)'
fairy_answer = "CinderellaCinderella"

def check_fairy_ans(ans):
    if ans.strip() == fairy_answer:
        return "โœจ Correct! Cinderella doubled!"
    else:
        return "โŒ Not quite, try again!"

# ------------------------
# ๐Ÿ’ป 6. SIMPLE CALCULATOR
# ------------------------

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y != 0:
        return x / y
    return "โŒ Cannot divide by zero!"

# ------------------------
# ๐ŸŽจ 7. GUESS THE NUMBER
# ------------------------

target_number = random.randint(1, 100)

def guess_number(guess):
    global target_number
    if guess < target_number:
        return "โฌ†๏ธ Try a higher number!"
    elif guess > target_number:
        return "โฌ‡๏ธ Try a lower number!"
    else:
        target_number = random.randint(1, 100)  # Reset after correct guess
        return "๐ŸŽ‰ Correct! Well done!"

# ------------------------
# ๐ŸŽจ UI
# ------------------------

with gr.Blocks(theme=gr.themes.Base(primary_hue="purple", secondary_hue="pink")) as app:
    gr.Markdown("## ๐Ÿง’โœจ Welcome to Python FunLand! ๐ŸŽฎ Learn Python by Playing")

    with gr.Tabs():
        with gr.TabItem("๐ŸŽฏ Guess the Output"):
            with gr.Row():
                score_box = gr.Textbox(label="๐Ÿ“Š Score", value="โญ Score: 0", interactive=False)
                hint_box = gr.Textbox(label="๐Ÿ’ก Hint", interactive=False)
            code_display = gr.Textbox(label="๐Ÿง  What will this Python code print?", interactive=False)
            options = gr.Radio(choices=[], label="Your Answer")
            result = gr.Textbox(label="๐ŸŽฏ Result", interactive=False)

            with gr.Row():
                gr.Button("โœ… Check Answer").click(fn=check_quiz_answer, inputs=options, outputs=[result, score_box])
                gr.Button("๐Ÿ’ก Get Hint").click(fn=quiz_hint, outputs=hint_box)
                gr.Button("โญ๏ธ Skip").click(fn=quiz_skip, outputs=[code_display, options, result, score_box])
                gr.Button("โช Back").click(fn=prev_quiz, outputs=[code_display, options, result, score_box])

            app.load(show_quiz_question, outputs=[code_display, options, result, score_box])

        with gr.TabItem("๐Ÿ”ค Word Scramble"):
            scramble_display = gr.Textbox(label="Unscramble This Word", interactive=False)
            word_input = gr.Textbox(label="Your Answer")
            scramble_result = gr.Textbox(label="Result", interactive=False)
            gr.Button("๐Ÿ”„ New Word").click(fn=show_scramble, outputs=[scramble_display, scramble_result])
            gr.Button("โœ… Submit").click(fn=check_scramble_answer, inputs=word_input, outputs=scramble_result)

        with gr.TabItem("๐ŸŽฒ Code Dice"):
            gr.Markdown("๐ŸŽฒ Click to roll a coding challenge!")
            dice_output = gr.Textbox(label="Challenge", interactive=False)
            gr.Button("๐ŸŽฒ Roll Dice").click(fn=roll_dice, outputs=dice_output)

        with gr.TabItem("๐Ÿงฉ Puzzle Blocks"):
            gr.Markdown("๐Ÿงฉ Arrange the code blocks in the correct order and paste below:")
            user_order = gr.Textbox(label="Paste Your Ordered Code", lines=5)
            puzzle_result = gr.Textbox(label="Result", interactive=False)
            gr.Button("โœ… Submit").click(fn=check_code_order, inputs=user_order, outputs=puzzle_result)

        with gr.TabItem("๐Ÿ“š Code Fairy Tales"):
            gr.Markdown("๐Ÿงš Imagine what this code prints:")
            gr.Textbox(label="Code", value=fairy_code, interactive=False)
            fairy_input = gr.Textbox(label="Your Answer")
            fairy_result = gr.Textbox(label="Result", interactive=False)
            gr.Button("โœ… Submit").click(fn=check_fairy_ans, inputs=fairy_input, outputs=fairy_result)

        with gr.TabItem("๐Ÿ’ป Simple Calculator"):
            gr.Markdown("๐Ÿงฎ Let's do some math!")
            num1 = gr.Number(label="Enter first number")
            num2 = gr.Number(label="Enter second number")
            operation = gr.Radio(choices=["Add", "Subtract", "Multiply", "Divide"], label="Choose Operation")
            calc_result = gr.Textbox(label="Result", interactive=False)
            gr.Button("โœ… Calculate").click(fn=lambda n1, n2, op: {
                "Add": add(n1, n2),
                "Subtract": subtract(n1, n2),
                "Multiply": multiply(n1, n2),
                "Divide": divide(n1, n2)
            }[operation], inputs=[num1, num2, operation], outputs=calc_result)

        with gr.TabItem("๐ŸŽฏ Guess the Number"):
            gr.Markdown("๐ŸŽฎ Guess the number between 1 and 100!")
            number_guess = gr.Number(label="Your Guess", interactive=True)
            guess_result = gr.Textbox(label="Result", interactive=False)
            gr.Button("โœ… Check Guess").click