EasyPython / app.py
keerthuAi's picture
Update app.py
5bbe733 verified
Raw
History Blame
1.66 kB
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()