I'm trying to validate user's input with a while loop so that only integers and number's greater than 8 are accepted. The code I have currently runs properly when a string is inputed and prints the correct output.
However, the problem I am running into is that when a user enters a integer less than 8 it displays the "Incorrect Input" message that I want, but when it prompts the user to enter another input. When I re-enter a integer greater than 8 it still prints "Incorrect Input" when I want it to break to the next prompt. Anyone have suggestions that I may be doing wrong? I'm not allowed to define a function for this particular assignment.
Number_List = []
while (True):
try:
Number_List.extend(map(int, input("Enter the length, width, and height of the room: ").split()))
except ValueError:
print("\nIncorrect Input. \nPlease try again. \n")
continue
if (Number_List[0] < 8) or (Number_List[1] < 8) or (Number_List[2] < 8):
print("\nIncorrect Input. \nPlease try again. \n")
continue
else:
break
It's because you are extending your list. So when you enter new inputs, the numbers are added at the end of Number_List
.
Therefore Number_List[0]
, Number_List[1]
, Number_List[2]
always stay the same.
To understand better, I advise you to print Number_List
before the except.
To make it works you could simply replace the corresponding line to:
Number_List = list((map(int, input("Enter the length, width, and height of the room: ").split())))
With that you will create a new list every time.
Hope it helped.