Spaces:
Build error
Build error
| 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?" | |
| } | |
| ] | |
| 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")] | |
| 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" | |
| ] | |
| 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!" | |
| # ------------------------ | |
| # ๐จ UI | |
| # ------------------------ | |
| with gr.Blocks(theme=gr.themes.Base(primary_hue="purple", secondary_hue="pink")) as app: | |
| gr.Markdown("## ๐งโจ Welcome to Python FunLand! ๐ฎ Learn 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) | |
| app.launch(server_name="0.0.0.0", server_port=7860) | |