keerthuAi commited on
Commit
1ba70c1
·
verified ·
1 Parent(s): 5bbe733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -29
app.py CHANGED
@@ -1,58 +1,67 @@
1
  import gradio as gr
2
  import random
3
 
4
- # Sample Python code questions
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
- current_q = random.choice(questions)
 
25
 
26
- def get_question():
27
- global current_q
28
- current_q = random.choice(questions)
29
- return current_q["code"], current_q["options"]
 
 
30
 
31
  def check_answer(choice):
32
  global score
33
- if choice == current_q["answer"]:
 
 
 
 
34
  score += 1
35
- return f"✅ Correct! 🎉 Your Score: {score}"
36
  else:
37
- return f"❌ Oops! Try again. Correct answer was: {current_q['answer']}"
38
-
39
- # Gradio UI
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
- def show_question():
49
- code, opts = get_question()
50
- return code, gr.update(choices=opts)
51
 
52
- btn_check.click(fn=check_answer, inputs=options, outputs=result)
53
- btn_next.click(fn=show_question, outputs=[code_display, options])
54
 
55
- # Load the first question on launch
56
- quiz.load(show_question, outputs=[code_display, options])
 
 
57
 
58
- quiz.launch()
 
 
 
 
 
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