Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
{
|
| 7 |
"code": 'print("Hello" + "World")',
|
| 8 |
"options": ["HelloWorld", "Hello World", "Hello+World"],
|
|
@@ -23,81 +26,164 @@ questions = [
|
|
| 23 |
}
|
| 24 |
]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
history = []
|
| 30 |
|
| 31 |
-
def
|
| 32 |
-
return
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
q =
|
| 36 |
-
return q["code"], gr.update(choices=q["options"]), "", f"โญ Score: {
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
global
|
| 40 |
-
correct =
|
| 41 |
result = ""
|
| 42 |
if choice == correct:
|
| 43 |
result = "โ
Correct! Great job!"
|
| 44 |
-
|
| 45 |
-
score += 1
|
| 46 |
else:
|
| 47 |
result = f"โ Oops! The correct answer was: {correct}"
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
""
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|