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!

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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."
W3Schools has a very helpful library of stuff about Python and other programming languages; here's their article about casting: w3schools.com/python/python_casting.asp
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?
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()." (realpython.com/len-python-function)
You gotta love it when Python is really picky like this, don't you?
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

1 Comment

Can you post some example using code to be more specific?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.