Spaces:
Build error
Build error
| 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 show_question() | |
| # ๐จ Gradio UI | |
| with gr.Blocks(theme=gr.themes.Base(primary_hue="green", secondary_hue="pink")) as app: | |
| gr.Markdown("## ๐ Python Fun Quiz Game for Kids ๐ฎ") | |
| with gr.Row(): | |
| score_box = gr.Textbox(label="๐ Your Score", interactive=False, value="โญ Score: 0") | |
| hint_box = gr.Textbox(label="๐ก Hint", interactive=False) | |
| 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="๐ฏ Result", interactive=False) | |
| with gr.Row(): | |
| btn_check = gr.Button("โ Check Answer") | |
| btn_hint = gr.Button("๐ก Get Hint") | |
| btn_skip = gr.Button("โญ๏ธ Next") | |
| btn_back = gr.Button("โช Back") | |
| btn_check.click(fn=check_answer, inputs=options, outputs=[result, score_box]) | |
| btn_hint.click(fn=get_hint, outputs=hint_box) | |
| btn_skip.click(fn=skip_question, outputs=[code_display, options, result, score_box]) | |
| btn_back.click(fn=prev_q, outputs=[code_display, options, result, score_box]) | |
| # Load first question | |
| app.load(show_question, outputs=[code_display, options, result, score_box]) | |
| gr.Markdown("---") | |
| gr.Markdown("## ๐ Coming Soon: Unique Games") | |
| gr.Markdown(""" | |
| - ๐ค **Python Word Scramble**: Rearrange jumbled Python keywords | |
| - ๐ฒ **Code Dice**: Roll a die to get random challenges | |
| - ๐งฉ **Puzzle Blocks**: Drag code blocks to form a working program | |
| - ๐งโโ๏ธ **Code Fairy Tales**: Read stories and guess what the code will do | |
| """) | |
| app.launch() | |