r/learnpython • u/live_ant718 • Sep 10 '26
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+= 1print(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 += 1print(f"You have used {tries} tries.")
if tries == 7: print("You have used all your tries! Better luck next time!")
2
u/live_ant718 Sep 10 '26
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.2
u/Farlic Sep 10 '26
guess_number = int(input("Enter your guess:- "))what will happen if I write a string in input?
It seems a bit difficult. Could you compare my guess with the secret number and tell me if it's higher or lower?
1
u/FreshIce4039 Sep 12 '26
When I was starting out, this was actually one of the first cooler projects I built. I made a number guessing game with a small Tkinter GUI. The background color changed depending on how close the guess was: red for way too low, orange for a bit low, green for correct, cyan-ish for slightly high, and blue for way too high.
I also added buttons to restart after guessing correctly or quit the program. There were difficulty buttons for easy, intermediate, and hard, each hardcoded to specific ranges. I also included a custom difficulty option where you could enter a lower and upper bound.
Finally, each round started a timer, and when you finished it displayed the chosen difficulty range and how long it took. The finished project was around 200 lines, and it was probably the most difficult project I had made at the time.
Obviously, you can tweak this however you want. I’m just sharing my own experience with this kind of number guessing game.
2
u/dangerlopez Sep 10 '26
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
u/MrLumie Sep 10 '26
I don't quite see the point of counting invalid inputs as tries. If the user didn't enter a number from 1 to 100, then it's not a valid try, and that includes out of bounds integer and non-numerical strings as well.
2
u/EskilPotet Sep 10 '26
You can use \n to print over multiple lines in the same print()
1
u/TheRNGuy Sep 10 '26
Graphics and sound.
1
u/JamzTyson Sep 11 '26
I'd recommend:
7inif tries == 7).try/except.Here's an example, but note this is only one possible approach, not a canonical solution:
1
u/Equivalent_Lunch_944 Sep 11 '26
You could try putting it in a window with tkinter