Having Problem with a Python Project

Hello everyone, This is my first time writing in this Forum. Would really Appreciate if anyone could help me with this code.What this code was supposed to do that the user has to guess a random no. between 1 to 10 and with the no of attempts the user did it would assign the user a phrase.Currently when i’m using it, it is not working as its suppose to.


numbers = random.randint(1,10)
attempts = 0
while True:
    try:
        guess = int(input("Enter your Guess: "))

        if numbers == guess:
            attempts = attempts + 1
            print("Guess in" + attempts + " attempts. ") 
            break
        elif numbers > guess:
            print("Enter a Higher Number")
            attempts = attempts + 1
        else:
            print("Enter a Lower Number")
            attempts = attempts + 1

        if attempts == 1:
            print("Intelligent Guess")
            break

        elif attempts <= 3:
            print("Not Bad")

        else:
            print("Bit More Luck Required")

    except:
            print("Choose No. between 1 to 100 ")


Heya! Welcome :smiley:

There are 2 issues that I can see with the code:

  1. An Exception is thrown when trying to concatenate a string and an Integer when the user correctly guesses the number.
  2. The second if statement will always evaluate to true when a user gets the number wrong on the first try because here attempt will always be 1. Maybe if you can shuffle this logic around a bit you can solve this so it behaves as intended.

Sidenote: Try to minimise the repetition of code, attempts = attempts + 1 get’s repeated 3 times whilst this really is not needed at all. Also do specify which Exception you are catching as an unexpected error might obfuscated due to a bare except clause (as is the case in your code :slight_smile: )

Good luck and have fun learning!

2 Likes

Thanks for the reply. I will try to change the code acc. to your suggestion.