Home > Enterprise >  Invalid literal for Int() base 10
Invalid literal for Int() base 10

Time:10-01

Here is the Code.

def number():
    messagebox_shown = False
    hand = lhvar.get()
    for value_widget in VALUES:
        value = value_widget.get()        
        if not messagebox_shown and value is not None:
            value = int(value) % 2 
            if hand == value:
                if hand == 0: 
                    messagebox.showinfo('Even', 'Part Number is Even, but L/H is selected')
                else:
                    messagebox.showinfo('Odd', 'Part Number is Odd, but R/H is selected')
                messagebox_shown = True

Here is the error'd out code:

invalid literal for int() with base 10: ''
  File "C:\project\session.py", line 49, in number
    value = int(value) % 2  # now change value to the remainder of itself mod 2
  File "C:\project\session.py", line 38, in inner_combined_func (Current frame)
    f(*args, **kwargs)
  File "C:\project\session.py", line 1248, in <module>
    root.mainloop()

Im trying to build a command that will tell the user that an error might have occured based off of a dropdown.

The VALUE has a list of a bunch of entry boxes which will all contain numbers. lhvar is the dropdown

How would I go about making it so it doesnt error out for the Int? Ive tried converting it to a float but it freaks out about that as well. But the result shouldn't ever be a decimal anyways.

CodePudding user response:

The issue appears to to be that value_widget.get() it probably returning an empty string, which is why you get an error when you try to turn that empty string into an integer (or float) there isn't anything to transform. To get around this, I would suggest adding a check for that on line 6. So the if statement would look like this:

if not messagebox_shown and value is not None and value != '':

CodePudding user response:

I think you are looking for a way to check if the number is odd/even and show messagebox based on hand selected. Which does not make sense on why if hand == value is being done. What you can do is:

def number():
    messagebox_shown = False
    hand = lhvar.get()

    for value_widget in VALUES:
        value = value_widget.get()        
        if not messagebox_shown:
            value = int(value) % 2
            
            if value == 0: # If value is even
                if hand == 'L/H': # If value is even and L/H is selected
                    messagebox.showinfo('Even', 'Part Number is Even, but L/H is selected')
            else: # If value is odd
                if hand == 'R/H': # If value is odd and R/H is selected
                    messagebox.showinfo('Odd', 'Part Number is Odd, but R/H is selected')
            messagebox_shown = True

Regardless, something like invalid literal for int() with base 10: '' should not happen unless value_widget is empty and if there is a case in which value_widget must be empty then you can use

if not messagebox_shown and value:
  • Related