"Guess the Number" game in Python I have been using Python 3 to learn programming, and this is a very basic task, but I want to gain good programming patterns/habits at the very beginning.

The program will first randomly generate a number unknown to the user. The user needs to guess what that number is. The program interacts with the user.

import random
randomNumber = random.randrange(0,100)
print("Random number has been generated")
guessed = False
while guessed==False:
    userInput = int(input("Your guess pleas: "))
    if userInput==randomNumber:
        guessed = True
        print("Well done!")
    elif userInput&gt;100:
        print("Our guess range is between 0 and 100, please try a bit lower")
    elif userInput&lt;0:
        print("Our guess range is between 0 and 100, please try a bit higher")
    elif userInput&gt;randomNumber:
        print("Try one more time, a bit lower")
    elif userInput &lt; randomNumber:
        print("Try one more time, a bit higher")

print("End of program")
I would probably combine these two if clauses:

elif userInput&gt;100:
    print("Our guess range is between 0 and 100, please try a bit lower")
elif userInput&lt;0:
    print("Our guess range is between 0 and 100, please try a bit higher")


To:

elif user_input &gt; 100 or user_input &lt; 0:
    print("Our guess range is between 0 and 100, please try again")


Trusting that the user is clever enough to realise that if he entered 1000 (which is still visible in the line just above this output), that he is too high for range(1,101).



Incidentally, there may be a small bug in your program:

random.randrange(1,100)


will never generate 100, just like range(1,100) will only give you the numbers from 1 to 99, inclusive. But you also allow the user to guess 0 or 100.

Use either of these two:

random.randrange(0,101)
random.randint(0,100)


The latter actually does return an integer N, such that a &lt;= N &lt;= b



Your program will crash if the user enters any non-number. Better use something like:

while True:
    try:
        user_input = int(input("Your guess please: "))
    except ValueError:
        print("Please enter a number")
        continue
    if user_input == random_number:
        print("You win")
        break
    elif user_input &lt; 0 or user_input &gt; 100:
        print("Our guess range is between 0 and 100, please try again")
    elif user_input &lt; random_number:
        # Notify user
    elif user_input &gt; random_number:
        # Notify user


Which will ask the user until a valid number is supplied. You can also put all the other checking code in there as well.