0

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!

superbo9y
  • 13
  • 3

2 Answers2

1

I think your issue might be that get() treats every user input as a string.

In other words, even though the user might want to say 22, the computer would read it as "22".

To safeguard against this, you can try putting int() around User_Age.get():

int(User_Age.get())

Hope this helps! -VDizz

VDizz
  • 34
  • 5
  • Side note: when you typecast, make sure that the string would actually be a whole number; int() will not save you from a string like "Hello, my name is Bill; I am 22 years old." – VDizz Apr 11 '22 at 14:38
  • W3Schools has a very helpful library of stuff about Python and other programming languages; here's their article about casting: https://www.w3schools.com/python/python_casting.asp – VDizz Apr 11 '22 at 14:39
  • Thank you. One more thing: one of the error handles is also to check the length of the field, to ensure the user has not left it empty. Because the `.get()` is now wrapped using `int` , a TypeError arises: `TypeError: object of type 'int' has no len()`. Do you see a way around this? – superbo9y Apr 11 '22 at 14:46
  • Now you have to typecast it *back* to a string, because len() doesn't work with integers, floats, doubles and stuff like that. To quote RealPython: "The integer, float, Boolean, and complex types are examples of built-in data types that you can’t use with len()." (https://realpython.com/len-python-function) – VDizz Apr 15 '22 at 23:38
  • You gotta love it when Python is really picky like this, don't you? – VDizz Apr 15 '22 at 23:38
0

Like @VDizz, I am assuming your values are coming from tk.Entry widgets. Therefore all values will be strings. So the casting will work but this will also generate an error in your elif block. To avoid the error causing a new issue you might try writing a small function to check the type. As per this stack overflow response https://stackoverflow.com/a/9591248/6893713.

Hope this helps too.

In response to @Guilherme Iazzete's comment (slightly modified from the link):

    def intTryParse(value):
        try:
            int(value)
            return True
        except ValueError:
            return False

    username = 'HarvB'
    age = '4'
    flag = False
    while flag == False:
        if len(username) == 0:
            messagebox.showerror('Project Pulse', '...')
            break
            ...
        elif intTryParse(age) is False:
            messagebox.showerror('Project Pulse', '...')
            break
            ...
        else:
            flag = True
HarvB
  • 50
  • 1
  • 7