[SOLVED] Python: Functions & Loops. Error: ValueError: invalid literal for int() with base 10:

Issue

I am learning Python functions and loops; however, I am running in to an error when trying to calculate two variables assigned to an integer within a loop, I post my code below as well as a draw.io diagram so better explain what I am trying to accomplish:

click here to view draw.io diagram
in the included image the green represents a working loop, or function and the red represent a broken loop or function, the white represents not yet coded

in the included code, the loops work on input option m and l, however the loop does not work on option q. the q option will also not work with additions with the w,e and r options.

P.S I have coded nothing in the w, e, r or y options yet (as you can see) however I have assigned integers to the q,w,e and r variables.

q = 1
w = 2
e = 3
r = 4

def exitprogram():
    print ("exiting...")
    
    

def q_function():
    Q_loop = True
    while Q_loop:
        print("Q function selected, select addition - press k to go back or x to exit")
        print("")
        Q_addition = int(input("Add Q with "))
        
        #exit program
        if Q_addition =="x":
            exitprogram()
            break
            
        
        #additions
        elif Q_addition == "w":
            q_w = q+w
            print(q_w)

        if Q_addition == "k":
            start()

        #invalid input
        else:
            print("invalid input - try again")
            continue
    
    
def w_function():
    print("W function operational")
    W_addition = int(input("Add W with - to exit select x "))
    
def e_function():
    print("E function operational")
    E_addition = int(input("Add E with - to exit select x "))

def r_function():
    print("R function operational")
    R_addition = int(input("Add R with - to exit select x "))

def t_function():
    T_loop = True
    while T_loop:
        T_selection = input("T function operational - press k to go back or x to exit ")
        if T_selection == "k":
            more()
        elif T_selection == "x":
            exitprogram()
            break
            
        
        else:
            print("invalid input - try again")
            continue

def y_function():
    print("Y function operational")

def more():
    moreloop = True
    while moreloop:
        l = input ("select t or y - to go back select k to exit select x ")
        if l =="t":
            t_function()
            break
        if l =="y":
            y_function()
            break
        if l =="x":
            exitprogram()
            break
        elif l =="k":
            start()
        else:
            print("invalid input - try again")
            continue

def start():
    loop1 = True
    while loop1:
        a = input("select q, w, e or y - for more options select m or to exit select x ")
        if a == "x":
            exitprogram()
            break
        elif a == "q":
            q_function()
            break 
        elif a == "w":
            w_function()
            break
        elif a == "e":
            e_function()
            break
        elif a == "r":
            r_function()
            break

        elif a =="m":
            more()
            break
              
            
        else:
            print("invalid input - try again")
            continue
start()     

Error traceback included below

 Traceback (most recent call last):
  File "x:/xxx/xxx/xxxx/xxxx.xx", line 115, in <module>
    start()
  File "x:/xxx/xxx/xxxx/xxxx.xx", line 95, in start
    q_function()
  File "x:/xxx/xxx/xxxx/xxxx.xx"", line 16, in q_function
    Q_addition = int(input("Add Q with "))
ValueError: invalid literal for int() with base 10: 'w'

Solution

You have Q_addition = int(input("Add Q with ")) so are attempting to cast the input (x, k, e, etc) to an int. This throws the error. Also your logic checks for strings not int

#exit program
if Q_addition =="x":
    Q_loop = False #set the loop variable to false to exit the loop and return to main function
#changed to elif as Q_addition can only be one option
elif Q_addition =="x":
...
elif Q_addition == "w":
#if you'd like addition with the other variables need to add them 
elif Q_addition == "e":
  q_e = q+e
  print(q_e)
elif Q_addition == "r":
  q_r = q+r
  print(q_r)
...
#changed to elif, see above
elif Q_addition == "k":

remove the int() cast so Q_addition = input("Add Q with ")

Answered By – depperm

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published.