I am trying to code a variable with a range from 4-10. The user inputs a number that will be determined if it's one of the numbers in the variable (4 to 10).
I tried using the following:
variable = range(4, 10)
#Then the user input:
number = input('Enter a number:')
if number == variable:
print(variable 'selected.')
This isn't working for me. It seems to just ignore these commands.
What command(s) do I need to use to create a variable range? Thanks.
The input function returns either a string (3.0+) or a number (if you enter one in 2.7 or before). The range function returns either a range object (3.0+) or a list (2.7-). None of those comparisons work.
Are you trying to get a random number that the user then guesses? If so, look at the random module, either random.randrange or random.randint.
If the user enters in, say 8, python see that the 8 is one of the numbers in the variable and runs the script contained in the "if" command. Same with any other number 4 to 10. Any ideas on how to do it?
you are trying to compare a single item to a list, it will never match.
def get_number():
number = 0
while number < 4 or number > 10:
try:
number = int(input('Please enter a number between 4 and 10: '))
except ValueError:
print("That's not a number")
return number
# try it
if __name__ == '__main__':
print('{} selected'.format(get_number()))
That seems to work! I'm still not very good at coding so your code is a little confusing. I'll look into all the commands you used. I hadn't used the greater/less than symbols very much so I'll have to start practicing with them. Thanks for the help.