First, to get an input from the user, depending on which version of python you use, you need to do the following:
For python 2:
user = raw_input("Enter a number between 0 and 6: ")
For python 3:
user = input("Enter a number between 0 and 6: ")
Now, to check if the value entered by the user is between 0 and 6, you have the following check:
if 0 <= int(user) <= 6:
...
OR
if int(user) in range(7):
...
OR
if int(user) >= 0 and int(user) <= 6:
...
All these are valid. It is important to note here that the user
will be a string, thus the int(user)
.
And then within the function, you can raise an exception if they enter a value that is not between 0 and 6 by:
raise ValueError("Invalid number input, must be within 0 and 6")
So in the end you would have something like this:
>>> import random
>>> pc_num = random.randint(0, 6) #Between 0 and 6, inclusive.
>>> while true:
user_num = int(input("Enter a number between 0 and 6: "))
if 0 <= user_num <= 6:
if user_num < pc_num:
print("low")
elif user_num > pc_num:
print("high")
else:
print("correct")
break
else:
raise ValueError("Invalid number input, must be within 0 and 6")