Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ def guess_number(guess, secret_number, attempts):
|
|
| 11 |
# Validate input
|
| 12 |
if not isinstance(guess, (int, float)) or not (1 <= guess <= 100):
|
| 13 |
result = "β οΈ Please enter an integer between 1 and 100."
|
| 14 |
-
return result, secret_number, attempts
|
| 15 |
|
| 16 |
attempts += 1 # Increment the number of attempts
|
| 17 |
|
|
@@ -20,25 +20,23 @@ def guess_number(guess, secret_number, attempts):
|
|
| 20 |
result = f"π₯ Game Over! You couldn't guess the number within 8 attempts. The number was {secret_number}. Starting a new game."
|
| 21 |
secret_number = None # Reset the game
|
| 22 |
attempts = 0
|
| 23 |
-
return result, secret_number, attempts, "background-color: lightcoral;" # Set background color for game over
|
| 24 |
elif guess == secret_number:
|
| 25 |
result = f"π Congratulations! You guessed the number {secret_number} in {attempts} attempt(s). Starting a new game."
|
| 26 |
secret_number = None # Reset the game
|
| 27 |
attempts = 0
|
| 28 |
-
return result, secret_number, attempts, "background-color: lightgreen; animation: confetti 1s forwards;" # Set background color for win
|
| 29 |
elif guess < secret_number:
|
| 30 |
result = f"π Too low! Attempt {attempts}/8."
|
| 31 |
else:
|
| 32 |
result = f"π Too high! Attempt {attempts}/8."
|
| 33 |
|
| 34 |
-
return result, secret_number, attempts
|
| 35 |
|
| 36 |
# Function to restart the game
|
| 37 |
def restart_game(secret_number, attempts):
|
| 38 |
secret_number = random.randint(1, 100)
|
| 39 |
attempts = 0
|
| 40 |
result = "π Game has been restarted! Try to guess the new number between 1 and 100."
|
| 41 |
-
return result, secret_number, attempts
|
| 42 |
|
| 43 |
# Function to reveal the answer
|
| 44 |
def reveal_answer(secret_number, attempts):
|
|
@@ -49,17 +47,9 @@ def reveal_answer(secret_number, attempts):
|
|
| 49 |
result = f"π The secret number was {secret_number}. Starting a new game."
|
| 50 |
secret_number = None # Reset the game
|
| 51 |
attempts = 0
|
| 52 |
-
return result, secret_number, attempts
|
| 53 |
|
| 54 |
-
|
| 55 |
-
css = """
|
| 56 |
-
@keyframes confetti {
|
| 57 |
-
0% {transform: translateY(0);}
|
| 58 |
-
100% {transform: translateY(-200px);}
|
| 59 |
-
}
|
| 60 |
-
"""
|
| 61 |
-
|
| 62 |
-
with gr.Blocks(css=css) as iface:
|
| 63 |
gr.Markdown("# π΅οΈββοΈ Number Guessing Game")
|
| 64 |
gr.Markdown("Try to guess the secret number between 1 and 100 in less than 8 attempts.")
|
| 65 |
|
|
@@ -82,21 +72,21 @@ with gr.Blocks(css=css) as iface:
|
|
| 82 |
submit_button.click(
|
| 83 |
fn=guess_number,
|
| 84 |
inputs=[guess_input, secret_number_state, attempts_state],
|
| 85 |
-
outputs=[output, secret_number_state, attempts_state
|
| 86 |
)
|
| 87 |
|
| 88 |
# Define what happens when the Restart button is clicked
|
| 89 |
restart_button.click(
|
| 90 |
fn=restart_game,
|
| 91 |
inputs=[secret_number_state, attempts_state],
|
| 92 |
-
outputs=[output, secret_number_state, attempts_state
|
| 93 |
)
|
| 94 |
|
| 95 |
# Define what happens when the Reveal Answer button is clicked
|
| 96 |
reveal_button.click(
|
| 97 |
fn=reveal_answer,
|
| 98 |
inputs=[secret_number_state, attempts_state],
|
| 99 |
-
outputs=[output, secret_number_state, attempts_state
|
| 100 |
)
|
| 101 |
|
| 102 |
iface.launch()
|
|
|
|
| 11 |
# Validate input
|
| 12 |
if not isinstance(guess, (int, float)) or not (1 <= guess <= 100):
|
| 13 |
result = "β οΈ Please enter an integer between 1 and 100."
|
| 14 |
+
return result, secret_number, attempts
|
| 15 |
|
| 16 |
attempts += 1 # Increment the number of attempts
|
| 17 |
|
|
|
|
| 20 |
result = f"π₯ Game Over! You couldn't guess the number within 8 attempts. The number was {secret_number}. Starting a new game."
|
| 21 |
secret_number = None # Reset the game
|
| 22 |
attempts = 0
|
|
|
|
| 23 |
elif guess == secret_number:
|
| 24 |
result = f"π Congratulations! You guessed the number {secret_number} in {attempts} attempt(s). Starting a new game."
|
| 25 |
secret_number = None # Reset the game
|
| 26 |
attempts = 0
|
|
|
|
| 27 |
elif guess < secret_number:
|
| 28 |
result = f"π Too low! Attempt {attempts}/8."
|
| 29 |
else:
|
| 30 |
result = f"π Too high! Attempt {attempts}/8."
|
| 31 |
|
| 32 |
+
return result, secret_number, attempts
|
| 33 |
|
| 34 |
# Function to restart the game
|
| 35 |
def restart_game(secret_number, attempts):
|
| 36 |
secret_number = random.randint(1, 100)
|
| 37 |
attempts = 0
|
| 38 |
result = "π Game has been restarted! Try to guess the new number between 1 and 100."
|
| 39 |
+
return result, secret_number, attempts
|
| 40 |
|
| 41 |
# Function to reveal the answer
|
| 42 |
def reveal_answer(secret_number, attempts):
|
|
|
|
| 47 |
result = f"π The secret number was {secret_number}. Starting a new game."
|
| 48 |
secret_number = None # Reset the game
|
| 49 |
attempts = 0
|
| 50 |
+
return result, secret_number, attempts
|
| 51 |
|
| 52 |
+
with gr.Blocks() as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Markdown("# π΅οΈββοΈ Number Guessing Game")
|
| 54 |
gr.Markdown("Try to guess the secret number between 1 and 100 in less than 8 attempts.")
|
| 55 |
|
|
|
|
| 72 |
submit_button.click(
|
| 73 |
fn=guess_number,
|
| 74 |
inputs=[guess_input, secret_number_state, attempts_state],
|
| 75 |
+
outputs=[output, secret_number_state, attempts_state]
|
| 76 |
)
|
| 77 |
|
| 78 |
# Define what happens when the Restart button is clicked
|
| 79 |
restart_button.click(
|
| 80 |
fn=restart_game,
|
| 81 |
inputs=[secret_number_state, attempts_state],
|
| 82 |
+
outputs=[output, secret_number_state, attempts_state]
|
| 83 |
)
|
| 84 |
|
| 85 |
# Define what happens when the Reveal Answer button is clicked
|
| 86 |
reveal_button.click(
|
| 87 |
fn=reveal_answer,
|
| 88 |
inputs=[secret_number_state, attempts_state],
|
| 89 |
+
outputs=[output, secret_number_state, attempts_state]
|
| 90 |
)
|
| 91 |
|
| 92 |
iface.launch()
|