How to use a variable across multiple functions

Question:

I am trying to use two variables (i, and q) across three functions. I want to test whether i and q are valid after each input . If they are then they will be added to list and the while loop continues. If they are not valid Then the function will stop and all input will be taken and presented. I have tried to make both i and q global variables but it does not seem to work as it is saying that "i" is not defined. How do I check the i and q variables in separate functions?

Any help would be really appreciated as I am only new and thought this should work.

I didn’t know what to add so I have put down the three functions in full below:

Updated:

   def add_item():
       code_inputed = []
       quan_inputed = []
       VC()
       VQ()
       vc_result = VC(I)
       vq_result = VQ(q)
       while True:    
           if i != "END":
               if vc_result == True and vq_result == True:
                   code_inputed.append(int(i))
                   quan_inputed.append(int(q))
               elif vc_result == True and vq_result == False:
                   print("Invalid Quanity")
                   break
               elif vc_result == False and vq_result == True:
                   print ("Invalid code")
                   break 
               else:
                   print("Invalid inputs")
                   break 
    return code_inputed,quan_inputed
        
def VC():
    i = input("enter code: ")
    minimum = 0
    maxiumum = 39 
    if i == "END":
        return False
    elif int(i) > minimum and int(i) <= maximum:
        True
    else:
        False
   
    
def VQ():
    q = input("enter quantity: ")
    minimum = 0
    maxiumum = 49
    if int(q) > minimum and int(q):
        True
    else:
        False

Thank you for any help

Asked By: Yonko_S

||

Answers:

Please read this W3Schools – Python global variables for a better understanding of how global variables work.

I think the problem with your code is, you should initialize your variables i and q first, before the add_item() function, and then get them from the global scope using the global keyword in the add_item function too.

i = 0
def add_item():
    global i

Plus, you want to recheck your add_item() conditions,

    while True:
        i = input("enter code: ")
        return i 
        if i != "END":
            q = input("enter quantity: ")
            return q # your code stops at this return
            VC()
            VQ()
            if VC == True and VQ == True:
                code_inputed.append(int(i))
                quan_inputed.append(int(q))
            elif VC == True and VQ == False:
                print("Invalid Quanity")
                break
            elif VC == False and VQ == True:
                print ("Invalid code")
                break 
            else:
                print("Invalid inputs")
                break 

P.S. you actually don’t have to use global variables in this context, you can just pass the values as functions arguments.

Answered By: Abdel

Looks like you need some help here. First of all the global keyword is for functions where you are setting the global variable. Also, forget about globals. Pass i and q to the functions directly. Then set those calls equal to variables and check those variables.

vc_result = VC(i)
vq_result = VQ(q)

if vc_result == True and vq_result == True:
  ...

Plus, return immediately exits the function so no processing will continue. Those should be removed. I recommend reviewing tutorials on python functions to fill in your knowledge.

Answered By: Dan Roberts