Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
# Sample
|
| 5 |
questions = [
|
| 6 |
{
|
| 7 |
"code": 'print("Hello" + "World")',
|
| 8 |
"options": ["HelloWorld", "Hello World", "Hello+World"],
|
| 9 |
-
"answer": "HelloWorld"
|
|
|
|
| 10 |
},
|
| 11 |
{
|
| 12 |
"code": 'print(2 * 3)',
|
| 13 |
"options": ["6", "23", "5"],
|
| 14 |
-
"answer": "6"
|
|
|
|
| 15 |
},
|
| 16 |
{
|
| 17 |
"code": 'x = 5\nprint(x + 2)',
|
| 18 |
"options": ["7", "52", "2"],
|
| 19 |
-
"answer": "7"
|
|
|
|
| 20 |
}
|
| 21 |
]
|
| 22 |
|
|
|
|
| 23 |
score = 0
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def check_answer(choice):
|
| 32 |
global score
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
score += 1
|
| 35 |
-
return f"✅ Correct! 🎉 Your Score: {score}"
|
| 36 |
else:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
with gr.Blocks(theme=gr.themes.Soft()) as quiz:
|
| 41 |
-
gr.Markdown("## 🧠 Python Quiz Game for Kids 🎮")
|
| 42 |
-
code_display = gr.Textbox(label="👇 What will this Python code print?", lines=2, interactive=False)
|
| 43 |
-
options = gr.Radio(choices=[], label="Choose your answer")
|
| 44 |
-
result = gr.Textbox(label="Your Result")
|
| 45 |
-
btn_check = gr.Button("Check Answer")
|
| 46 |
-
btn_next = gr.Button("Next Question")
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
+
# 🧠 Sample questions for kids
|
| 5 |
questions = [
|
| 6 |
{
|
| 7 |
"code": 'print("Hello" + "World")',
|
| 8 |
"options": ["HelloWorld", "Hello World", "Hello+World"],
|
| 9 |
+
"answer": "HelloWorld",
|
| 10 |
+
"hint": "What happens when you add two strings?"
|
| 11 |
},
|
| 12 |
{
|
| 13 |
"code": 'print(2 * 3)',
|
| 14 |
"options": ["6", "23", "5"],
|
| 15 |
+
"answer": "6",
|
| 16 |
+
"hint": "Multiplication or joining?"
|
| 17 |
},
|
| 18 |
{
|
| 19 |
"code": 'x = 5\nprint(x + 2)',
|
| 20 |
"options": ["7", "52", "2"],
|
| 21 |
+
"answer": "7",
|
| 22 |
+
"hint": "What is 5 + 2?"
|
| 23 |
}
|
| 24 |
]
|
| 25 |
|
| 26 |
+
# 🔁 Track state
|
| 27 |
score = 0
|
| 28 |
+
question_index = 0
|
| 29 |
+
history = []
|
| 30 |
|
| 31 |
+
def get_current_question():
|
| 32 |
+
return questions[question_index]
|
| 33 |
+
|
| 34 |
+
def show_question():
|
| 35 |
+
q = get_current_question()
|
| 36 |
+
return q["code"], gr.update(choices=q["options"]), "", f"⭐ Score: {score}"
|
| 37 |
|
| 38 |
def check_answer(choice):
|
| 39 |
global score
|
| 40 |
+
correct = get_current_question()["answer"]
|
| 41 |
+
result = ""
|
| 42 |
+
if choice == correct:
|
| 43 |
+
result = "✅ Correct! Great job!"
|
| 44 |
+
score_msg = f"⭐ Score: {score + 1}"
|
| 45 |
score += 1
|
|
|
|
| 46 |
else:
|
| 47 |
+
result = f"❌ Oops! The correct answer was: {correct}"
|
| 48 |
+
score_msg = f"⭐ Score: {score}"
|
| 49 |
+
return result, score_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
def skip_question():
|
| 52 |
+
next_q()
|
| 53 |
+
return show_question()
|
| 54 |
|
| 55 |
+
def get_hint():
|
| 56 |
+
return f"💡 Hint: {get_current_question()['hint']}"
|
| 57 |
|
| 58 |
+
def next_q():
|
| 59 |
+
global question_index
|
| 60 |
+
history.append(question_index)
|
| 61 |
+
question_index = (question_index + 1) % len(questions)
|
| 62 |
|
| 63 |
+
def prev_q():
|
| 64 |
+
global question_index
|
| 65 |
+
if history:
|
| 66 |
+
question_index = history.pop()
|
| 67 |
+
return
|