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
face
is not same asnumber_of_faces
. Also an unconditionalbreak
means you are not having any loop there. – Austin