keerthuAi commited on
Commit
96f5111
ยท
verified ยท
1 Parent(s): f2f5a8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -71
app.py CHANGED
@@ -1,8 +1,11 @@
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"],
@@ -23,81 +26,164 @@ questions = [
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 show_question()
68
-
69
- # ๐ŸŽจ Gradio UI
70
- with gr.Blocks(theme=gr.themes.Base(primary_hue="green", secondary_hue="pink")) as app:
71
- gr.Markdown("## ๐Ÿ Python Fun Quiz Game for Kids ๐ŸŽฎ")
72
- with gr.Row():
73
- score_box = gr.Textbox(label="๐Ÿ“Š Your Score", interactive=False, value="โญ Score: 0")
74
- hint_box = gr.Textbox(label="๐Ÿ’ก Hint", interactive=False)
75
- code_display = gr.Textbox(label="๐Ÿง  What will this Python code print?", lines=2, interactive=False)
76
- options = gr.Radio(choices=[], label="๐Ÿง Choose your answer")
77
- result = gr.Textbox(label="๐ŸŽฏ Result", interactive=False)
78
-
79
- with gr.Row():
80
- btn_check = gr.Button("โœ… Check Answer")
81
- btn_hint = gr.Button("๐Ÿ’ก Get Hint")
82
- btn_skip = gr.Button("โญ๏ธ Next")
83
- btn_back = gr.Button("โช Back")
84
-
85
- btn_check.click(fn=check_answer, inputs=options, outputs=[result, score_box])
86
- btn_hint.click(fn=get_hint, outputs=hint_box)
87
- btn_skip.click(fn=skip_question, outputs=[code_display, options, result, score_box])
88
- btn_back.click(fn=prev_q, outputs=[code_display, options, result, score_box])
89
-
90
- # Load first question
91
- app.load(show_question, outputs=[code_display, options, result, score_box])
92
-
93
- gr.Markdown("---")
94
- gr.Markdown("## ๐ŸŒŸ Coming Soon: Unique Games")
95
- gr.Markdown("""
96
- - ๐Ÿ”ค **Python Word Scramble**: Rearrange jumbled Python keywords
97
- - ๐ŸŽฒ **Code Dice**: Roll a die to get random challenges
98
- - ๐Ÿงฉ **Puzzle Blocks**: Drag code blocks to form a working program
99
- - ๐Ÿงšโ€โ™€๏ธ **Code Fairy Tales**: Read stories and guess what the code will do
100
- """)
101
-
102
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
 
 
1
  import gradio as gr
2
  import random
3
 
4
+ # ------------------------
5
+ # ๐ŸŽฎ 1. GUESS THE OUTPUT QUIZ
6
+ # ------------------------
7
+
8
+ quiz_questions = [
9
  {
10
  "code": 'print("Hello" + "World")',
11
  "options": ["HelloWorld", "Hello World", "Hello+World"],
 
26
  }
27
  ]
28
 
29
+ quiz_score = 0
30
+ quiz_index = 0
31
+ quiz_history = []
 
32
 
33
+ def get_quiz_question():
34
+ return quiz_questions[quiz_index]
35
 
36
+ def show_quiz_question():
37
+ q = get_quiz_question()
38
+ return q["code"], gr.update(choices=q["options"]), "", f"โญ Score: {quiz_score}"
39
 
40
+ def check_quiz_answer(choice):
41
+ global quiz_score
42
+ correct = get_quiz_question()["answer"]
43
  result = ""
44
  if choice == correct:
45
  result = "โœ… Correct! Great job!"
46
+ quiz_score += 1
 
47
  else:
48
  result = f"โŒ Oops! The correct answer was: {correct}"
49
+ return result, f"โญ Score: {quiz_score}"
50
+
51
+ def quiz_skip():
52
+ next_quiz()
53
+ return show_quiz_question()
54
+
55
+ def quiz_hint():
56
+ return f"๐Ÿ’ก Hint: {get_quiz_question()['hint']}"
57
+
58
+ def next_quiz():
59
+ global quiz_index
60
+ quiz_history.append(quiz_index)
61
+ quiz_index = (quiz_index + 1) % len(quiz_questions)
62
+
63
+ def prev_quiz():
64
+ global quiz_index
65
+ if quiz_history:
66
+ quiz_index = quiz_history.pop()
67
+ return show_quiz_question()
68
+
69
+ # ------------------------
70
+ # ๐Ÿ”ค 2. WORD SCRAMBLE GAME
71
+ # ------------------------
72
+
73
+ scramble_words = [("python", "A popular programming language"),
74
+ ("loop", "Used to repeat code"),
75
+ ("print", "Outputs something on screen")]
76
+
77
+ def scramble_word(word):
78
+ return ''.join(random.sample(word, len(word)))
79
+
80
+ current_scramble = random.choice(scramble_words)
81
+
82
+ def show_scramble():
83
+ global current_scramble
84
+ current_scramble = random.choice(scramble_words)
85
+ return scramble_word(current_scramble[0]), ""
86
+
87
+ def check_scramble_answer(ans):
88
+ correct = current_scramble[0]
89
+ if ans.lower() == correct:
90
+ return "โœ… Awesome! That's correct!"
91
+ else:
92
+ return f"โŒ Nope! The right word was '{correct}'."
93
+
94
+ # ------------------------
95
+ # ๐ŸŽฒ 3. CODE DICE
96
+ # ------------------------
97
+
98
+ dice_challenges = [
99
+ "๐Ÿ’ก Create a variable and assign it your name",
100
+ "๐Ÿ’ก Write code to add 2 + 3 and print it",
101
+ "๐Ÿ’ก Use a loop to print numbers from 1 to 5",
102
+ "๐Ÿ’ก Print your favorite color 3 times"
103
+ ]
104
+
105
+ def roll_dice():
106
+ return random.choice(dice_challenges)
107
+
108
+ # ------------------------
109
+ # ๐Ÿงฉ 4. PUZZLE BLOCKS (Ordering)
110
+ # ------------------------
111
+
112
+ puzzle_code = [
113
+ "x = 5",
114
+ "y = 3",
115
+ "z = x + y",
116
+ "print(z)"
117
+ ]
118
+
119
+ correct_order = "\n".join(puzzle_code)
120
+
121
+ def check_code_order(user_code):
122
+ if user_code.strip() == correct_order:
123
+ return "โœ… Perfect order! You built it right!"
124
+ else:
125
+ return "โŒ Hmm... Try again! The order seems off."
126
+
127
+ # ------------------------
128
+ # ๐Ÿ“š 5. CODE FAIRY TALE
129
+ # ------------------------
130
+
131
+ fairy_code = 'print("Cinderella" * 2)'
132
+ fairy_answer = "CinderellaCinderella"
133
+
134
+ def check_fairy_ans(ans):
135
+ if ans.strip() == fairy_answer:
136
+ return "โœจ Correct! Cinderella doubled!"
137
+ else:
138
+ return "โŒ Not quite, try again!"
139
+
140
+ # ------------------------
141
+ # ๐ŸŽจ UI
142
+ # ------------------------
143
+
144
+ with gr.Blocks(theme=gr.themes.Base(primary_hue="purple", secondary_hue="pink")) as app:
145
+ gr.Markdown("## ๐Ÿง’โœจ Welcome to Python FunLand! ๐ŸŽฎ Learn by Playing")
146
+
147
+ with gr.Tabs():
148
+ with gr.TabItem("๐ŸŽฏ Guess the Output"):
149
+ with gr.Row():
150
+ score_box = gr.Textbox(label="๐Ÿ“Š Score", value="โญ Score: 0", interactive=False)
151
+ hint_box = gr.Textbox(label="๐Ÿ’ก Hint", interactive=False)
152
+ code_display = gr.Textbox(label="๐Ÿง  What will this Python code print?", interactive=False)
153
+ options = gr.Radio(choices=[], label="Your Answer")
154
+ result = gr.Textbox(label="๐ŸŽฏ Result", interactive=False)
155
+
156
+ with gr.Row():
157
+ gr.Button("โœ… Check Answer").click(fn=check_quiz_answer, inputs=options, outputs=[result, score_box])
158
+ gr.Button("๐Ÿ’ก Get Hint").click(fn=quiz_hint, outputs=hint_box)
159
+ gr.Button("โญ๏ธ Skip").click(fn=quiz_skip, outputs=[code_display, options, result, score_box])
160
+ gr.Button("โช Back").click(fn=prev_quiz, outputs=[code_display, options, result, score_box])
161
+
162
+ app.load(show_quiz_question, outputs=[code_display, options, result, score_box])
163
+
164
+ with gr.TabItem("๐Ÿ”ค Word Scramble"):
165
+ scramble_display = gr.Textbox(label="Unscramble This Word", interactive=False)
166
+ word_input = gr.Textbox(label="Your Answer")
167
+ scramble_result = gr.Textbox(label="Result", interactive=False)
168
+ gr.Button("๐Ÿ”„ New Word").click(fn=show_scramble, outputs=[scramble_display, scramble_result])
169
+ gr.Button("โœ… Submit").click(fn=check_scramble_answer, inputs=word_input, outputs=scramble_result)
170
+
171
+ with gr.TabItem("๐ŸŽฒ Code Dice"):
172
+ gr.Markdown("๐ŸŽฒ Click to roll a coding challenge!")
173
+ dice_output = gr.Textbox(label="Challenge", interactive=False)
174
+ gr.Button("๐ŸŽฒ Roll Dice").click(fn=roll_dice, outputs=dice_output)
175
+
176
+ with gr.TabItem("๐Ÿงฉ Puzzle Blocks"):
177
+ gr.Markdown("๐Ÿงฉ Arrange the code blocks in the correct order and paste below:")
178
+ user_order = gr.Textbox(label="Paste Your Ordered Code", lines=5)
179
+ puzzle_result = gr.Textbox(label="Result", interactive=False)
180
+ gr.Button("โœ… Submit").click(fn=check_code_order, inputs=user_order, outputs=puzzle_result)
181
+
182
+ with gr.TabItem("๐Ÿ“š Code Fairy Tales"):
183
+ gr.Markdown("๐Ÿงš Imagine what this code prints:")
184
+ gr.Textbox(label="Code", value=fairy_code, interactive=False)
185
+ fairy_input = gr.Textbox(label="Your Answer")
186
+ fairy_result = gr.Textbox(label="Result", interactive=False)
187
+ gr.Button("โœ… Submit").click(fn=check_fairy_ans, inputs=fairy_input, outputs=fairy_result)
188
 
189
+ app.launch(server_name="0.0.0.0", server_port=7860)