Follow PEP8
PEP8 specifies that variables should be named like functions, using _underscores_ over mixedCase:
randomNumber
-> random_number
userInput
-> user_input
Use chained comparisons
Building on Graipher's point, I would negate the condition and take advantage of Python's chained comparison operators:
elif not 0 <= user_input <= 100:
print("Our guess range is between 0 and 100, please try again")
Keep your code DRY
2 sets of strings in your code are very similar to each other. The conditional branches that bring you there are also similar and compare against the same variables.
In those cases, you can combine the branch conditions and strings together and branch only at the point when the strings differ, minimizing the duplication between strings. str.format()
or the % operator along with conditional expressions are your friends here:
elif not 0 <= user_input <= 100: # Combines 2 branches into 1
print("Our guess range is between 0 and 100, please try a bit {}."
# ^
# The strings only starts to differ based on the condition here
.format("higher" if user_input < 0 else "lower"))
Add compatibility checks
Although you were only referring to Python 3 for this exercise, it's a good idea to add compatibility for Python 2 for real applications.
Simply redefining input() is enough in this simple program:
if sys.version_info[0] < 3:
input = raw_input
For more complicated programs, I suggest you look into six.
Group code into functions
This is helpful even for a small program since it prevents too many variables from polluting the module scope and allows your module to be importable from another program. It's also easier to read and extend later on.
(In the example below, you can easily add a range_min
and range_max
parameter to start_guess()
to easily tune the range of numbers to guess from)
Use is False
instead of == False
if necessary
You should generally avoid direct comparisons to a Boolean and just use not <variable>
as Toastrackenigma said if you know the possible values of the variable.
However, in cases where the variable value can also be an int, which is probably a sign of bad programming practice anyway, it's better to use is False
over == False
because 0 == False
.
(This is not needed in the sample below)
Updated code with mine and everyone's suggestions:
import random
import sys
# Compatibility with Python 2
if sys.version_info[0] < 3:
input = raw_input
def start_guess():
random_number = random.randrange(0, 101)
print("Random number has been generated.")
while True:
try:
user_input = int(input("Your guess please: "))
except ValueError:
print("Please enter a number.")
continue
if user_input == random_number:
print("Well done!")
break
elif not 0 <= user_input <= 100: # If it's not between 0 to 100
print("Our guess range is between 0 and 100, please try a bit {}."
# User_input at this point must be outside the range
# so if it's not below the min, it's definitely above the max
.format("higher" if user_input < 0 else "lower"))
else:
print("Try one more time, a bit {}."
# User_input at this point must be inside the range
# so if it's not below the random_number, it's definitely above it
.format("higher" if user_input < random_number else "lower"))
if __name__ == '__main__':
start_guess()
print("End of program.")
"Your guess pleas: "
: you're missing ane
inplease
. \$\endgroup\$