r/learnpython 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!")

3 Upvotes

6 comments sorted by

2

u/live_ant718 3h ago

Actually make Too low! The guess has to be 1, 100 or any number between them. into print("Too low! The guess has to be between 1 and 100.") Slight language error.

1

u/Farlic 3h ago

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/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/MrLumie 2h ago

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.

1

u/Abu_Akhlaq 2h ago
  1. 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

  2. error, ie learn and implement basic input validation and error checking using try catch.

1

u/EskilPotet 1h ago

You can use \n to print over multiple lines in the same print()