Spaces:
Build error
Build error
| 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() | |