-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
33 lines (29 loc) · 1.29 KB
/
app.py
File metadata and controls
33 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random
def start_game():
# Generate a random number between 1 and 10 for the user to guess
number_to_guess = random.randint(1, 10)
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 10.")
# Loop indefinitely until the user guesses the correct number
while True:
try:
# Prompt the user for their guess
guess_input = input("Enter your guess: ")
# Convert the user's input from a string to an integer
guess = int(guess_input)
except ValueError:
# If the user enters a non-numeric value, inform them and restart the loop
print("Invalid input. Please enter a whole number.")
continue # Continue to the next iteration of the loop, prompting for input again
# Compare the user's guess to the secret number
if guess < number_to_guess:
print("Too low!")
elif guess > number_to_guess:
print("Too high!")
else:
# If the guess is correct, congratulate the user and end the loop
print(f"You got it! The number was {number_to_guess}")
break
# This block ensures the game starts only when the script is executed directly
if __name__ == "__main__":
start_game()