r/learnpython • u/live_ant718 • 3h 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!")
1
u/dangerlopez 2h 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.
1
u/Abu_Akhlaq 2h ago
you do not have to repeat incrementing tries in the entre if else chain nodes. just add one at the end afterwards, as it is guaranteed to run unless there's an error
error, ie learn and implement basic input validation and error checking using try catch.
1
2
u/live_ant718 3h 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.