1
votes

im trying to create a dice game, the first thing I want to do is have the user input how many sides the dice has. I then want to validate that it's between 2-20 inclusive and if it's invalid display an error message and ask them for the input again until the input is valid.

It seems to always not accept the first input regardless if valid/invalid and then accepts the 2nd input even if its not valid

The output is below

face = int( input( "how many sides does your dice have? enter number between 2-20 inclusive: "))
validNumber = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

while face != validNumber:
   # print error message
   print("Im Sorry, Please enter a valid positive integer and try again")
   # ask for input again
   number_of_faces = int(input("How many sides do your dice have? Must be between 2 and 20 inclusive: "))
   break

print("Continuing with rest of program") 

Output:

how many sides does your dice have? enter number between 2-20 inclusive: 2 Im Sorry, Please enter a valid positive integer and try again How many sides do your dice have? Must be between 2 and 20 inclusive: 3 Continuing with rest of program

test2 how many sides does your dice have? enter number between 2-20 inclusive: 21 Im Sorry, Please enter a valid positive integer and try again How many sides do your dice have? Must be between 2 and 20 inclusive: 22 Continuing with rest of program

test3

how many sides does your dice have? enter number between 2-20 inclusive: 5 Im Sorry, Please enter a valid positive integer and try again How many sides do your dice have? Must be between 2 and 20 inclusive: 6 Continuing with rest of program

1
face is not same as number_of_faces. Also an unconditional break means you are not having any loop there.Austin

1 Answers

0
votes

The input will never be equal to the list. You have to check wether the input is in the list. Try:

face = int( input( "how many sides does your dice have? enter number between 2-20 inclusive: "))
validNumber = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

while face not in validNumber:
   # print error message
   print("Im Sorry, Please enter a valid positive integer and try again")
   # ask for input again
   face = int(input("How many sides do your dice have? Must be between 2 and 20 inclusive: "))

print("Continuing with rest of program")