The code given works fine but not with the original code.
I would like the code to offer a number 1 through 5 and only accept a number 1 through 5.
The number choosen Within range would still return random integers.
import random
user_input = raw_input("Enter a number between 1 and 5 : ")
selected_elem = []
while len(selected_elem) < int(user_input):
if user_input >= int(6):
print ("That is not an option...")
random_elem = random.randrange(1, 10, 1)
if random_elem not in selected_elem:
selected_elem.append(random_elem)
print ("Here are the numbers... ")+ str(selected_elem)
UPDATE with OP code posted now:
Note that if the input to
user_inputis not a number andint()fails your program will crash (throw an exception). You can graft on the try/except code shown below to deal with that if necessary.—— Previous answer w/o code posted by OP ————
If you can be sure the input will always be a number you can do this:
It will continue to loop until the user enters a number in the specified range.
Otherwise, the added try/except code will catch non-numeric input:
If you don’t want the user to try again after entering a non-number, just adjust the message and add a
breakstatement below the finalprintto exit the loop.