Continuing from yesterday, i was implementing a basic exception handler, to deal with every type of error from the user, using conditional if
and elif
statements. It was fine until my program wrongly thinks that integer inputs are non-integers (for the age
field), and this stops me from continuing my program, as other functions are dependant on this. Here is the code snippet:
def submit():
username = UserName.get()
firstname = User_FirstName.get()
surname = User_Surname.get()
age = User_Age.get()
height = User_Height.get()
weight = User_Weight.get()
data = [username, firstname, surname, age, height, weight]
flag = False
while flag == False:
if len(username) == 0:
messagebox.showerror('Project Pulse', 'Please ensure the "Username" field is not left blank')
break
elif type(firstname) != str:
messagebox.showerror('Project Pulse', 'Please ensure there are no numbers in the "First Name"')
break
elif len(firstname) == 0:
messagebox.showerror('Project Pulse', 'Please ensure that the "First Name" field is not left blank')
break
elif len(surname) == 0:
messagebox.showerror('Project Pulse', 'Please ensure that the "Last Name" field is not left blank')
break
elif type(surname) != str:
messagebox.showerror('Project Pulse', 'Please ensure there are no numbers in the "Surname"')
break
elif len(age) == 0:
messagebox.showerror('Project Pulse', 'Please ensure the "age" field is not left blank')
break
elif type(age) != int:
messagebox.showerror('Project Pulse', 'Please ensure only integers are input in "Age"')
break
...
else:
flag = True
Here, submit is a button. The elif
statements continue for a few more lines, but the point is, the program does not run past the 'age' line, i.e. the error message box : 'Please ensure only integers are input in "Age"
displays, no matter what. I tried printing the actual age variable, and i got an integer, so I can't seem to find the issue!