Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def check_guess(user_guess):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return f"π» Too low! Attempts: {gr.get_session_state('attempts')}"
|
| 17 |
-
elif user_guess > secret:
|
| 18 |
-
return f"πΊ Too high! Attempts: {gr.get_session_state('attempts')}"
|
| 19 |
else:
|
| 20 |
-
|
| 21 |
-
# Reset
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
-
gr.Markdown("## π― Guess the Number Game\nGuess a number between 1 and 100
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
+
def start_game():
|
| 5 |
+
secret_number = random.randint(1, 100)
|
| 6 |
+
attempts = 0
|
| 7 |
+
message = "Game started! Guess a number between 1 and 100."
|
| 8 |
+
return message, secret_number, attempts
|
| 9 |
+
|
| 10 |
+
def check_guess(user_guess, secret_number, attempts):
|
| 11 |
+
attempts += 1
|
| 12 |
+
if user_guess < secret_number:
|
| 13 |
+
message = f"π» Too low! Attempts: {attempts}"
|
| 14 |
+
elif user_guess > secret_number:
|
| 15 |
+
message = f"πΊ Too high! Attempts: {attempts}"
|
|
|
|
|
|
|
|
|
|
| 16 |
else:
|
| 17 |
+
message = f"π Correct! The number was {secret_number}. You got it in {attempts} tries!"
|
| 18 |
+
# Reset game
|
| 19 |
+
secret_number = random.randint(1, 100)
|
| 20 |
+
attempts = 0
|
| 21 |
+
message += " Game reset. Try a new number!"
|
| 22 |
+
|
| 23 |
+
return message, secret_number, attempts
|
| 24 |
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("## π― Guess the Number Game\nGuess a number between 1 and 100")
|
| 27 |
+
|
| 28 |
+
with gr.Row():
|
| 29 |
+
guess_input = gr.Number(label="Your Guess", precision=0)
|
| 30 |
+
guess_button = gr.Button("Submit Guess")
|
| 31 |
+
new_game_button = gr.Button("π New Game")
|
| 32 |
+
|
| 33 |
+
result_output = gr.Textbox(label="Result")
|
| 34 |
+
|
| 35 |
+
# State variables
|
| 36 |
+
secret_state = gr.State()
|
| 37 |
+
attempts_state = gr.State()
|
| 38 |
+
|
| 39 |
+
# Actions
|
| 40 |
+
guess_button.click(
|
| 41 |
+
check_guess,
|
| 42 |
+
inputs=[guess_input, secret_state, attempts_state],
|
| 43 |
+
outputs=[result_output, secret_state, attempts_state]
|
| 44 |
+
)
|
| 45 |
|
| 46 |
+
new_game_button.click(
|
| 47 |
+
start_game,
|
| 48 |
+
inputs=[],
|
| 49 |
+
outputs=[result_output, secret_state, attempts_state]
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
# Initialize game
|
| 53 |
+
demo.load(start
|