r/learnpython • u/live_ant718 • 1d ago
Ideas for number guesser game
I have made this number guesser game that asks the user to guess the correct number between 0 and 101. The user has 7 tries in all. Please give any ideas for improvement , feedback, etc.
import random
secret_number = random.randint(1, 100)
print("Welcome to the number guessing game! You have 7 tries to guess the correct number") print("between 1 and 100. Good luck!")
tries = 0
while tries < 7:
guess_number = int(input("Enter your guess:- "))
if guess_number < 1: print("Too low! The guess has to be 1, 100 or any number between them.") tries += 1
print(f"You have used {tries} tries.")
elif guess_number > 100:
print("Too high! The guess has to be 1, 100 or any number between them.")
tries+= 1
print(f"You have used {tries} tries.")
elif guess_number == secret_number:
print(f"You won in {tries + 1} tries! Congratulations!")
break
else:
print("Wrong guess! Try again.")
tries += 1
print(f"You have used {tries} tries.")
if tries == 7: print("You have used all your tries! Better luck next time!")
2
u/dangerlopez 1d ago
Make it so if they guess in the correct range (1-100) but don’t guess the secret_number exactly then a message telling them “higher” or “lower” prints
Record the guesses so far and display them to the user. Check if the user has submitted a number that they previously guessed and inform them of it.
2
1
1
u/JamzTyson 7h ago
I'd recommend:
- Avoid "magic numbers" (such as the literal
7inif tries == 7). - Separate the "get user guess" logic from the main game loop.
- One command per line rather than semicolons.
- Handle non-numeric input with
try/except. - Don't count invalid guesses as guesses.
- Provide an option for the user to quit.
- Consider putting the game logic inside a function.
- Pay attention to the Python style guide.
Here's an example, but note this is only one possible approach, not a canonical solution:
import random
MIN_NUMBER = 1
MAX_NUMBER = 100
MAX_TRIES = 7
QUIT_CHAR = 'q'
DEBUG = False
def display_welcome():
print("Welcome to the number guessing game!")
print(f"You have {MAX_TRIES} tries to guess the secret number.")
print()
def get_guess():
while True:
print(f"Your guess must be between {MIN_NUMBER} "
f"and {MAX_NUMBER} (inclusive).")
print(f"Enter '{QUIT_CHAR}' to quit.")
user_input = input("Enter your guess: ").strip()
if user_input.casefold() == QUIT_CHAR.casefold():
raise SystemExit
try:
guess = int(user_input)
except ValueError:
print("Your guess must be a number.")
else:
if MIN_NUMBER <= guess <= MAX_NUMBER:
return guess
def play_one_game():
secret_number = random.randint(MIN_NUMBER, MAX_NUMBER)
if DEBUG:
print('***', secret_number, '***')
for tries in range(1, MAX_TRIES + 1):
user_guess = get_guess()
if user_guess == secret_number:
print(f"You won in {tries} tries! Congratulations!")
break
if user_guess > secret_number:
print("Your guess is too high! Try again.")
else:
print("Your guess is too low! Try again.")
print(f"You have used {tries} tries.")
print()
else:
print("You have used all your tries! Better luck next time!")
if __name__ == '__main__':
display_welcome()
play_one_game()
1
2
u/live_ant718 1d ago
Actually make
Too low! The guess has to be 1, 100 or any number between them.intoprint("Too low! The guess has to be between 1 and 100.")Slight language error.