-1

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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Yonko_S
  • 11
  • 3

2 Answers2

1

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.

Abdel
  • 404
  • 2
  • 8
0

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.

Dan Roberts
  • 4,664
  • 3
  • 34
  • 43
  • Thank you for the fast reply. Using this method do I also need to def VC(i): as well and do i set vc_result = VC(i) outside of the function. I have tried adding the vc_result = VC(I) inside the add_item function and still says I is not defined. Where do I place it sorry – Yonko_S Sep 24 '22 at 02:11
  • I have edited the question to what I have written. Is this what you meant by? Thank you – Yonko_S Sep 24 '22 at 02:54
  • @Yonko_S you will define the function argument in order to pass it in like `def VC(i)`. Im not sure the scenario here, but you generally want to avoid using global variables. I recommend adding arguments `def add_item(i, q)`. That allows you to call the function and pass those specific values. Also, watch your capitalization of i. – Dan Roberts Sep 24 '22 at 03:04
  • Sorry, i'm a fool. That makes complete sense thank you – Yonko_S Sep 24 '22 at 03:10
  • @Yonko_S you can ignore my help here. I was not at a computer and didnt take time to reason out what you are trying to do. I think more work may be needed. Unless I'm actually helping lol – Dan Roberts Sep 24 '22 at 03:10
  • yeah, I misunderstood what you were trying to do. You want to get input from the functions. In add_item() remove VC() and VQ(), rename _result variables to q and i, then move those two lines within the while. The outcome of that is that on each loop you will get new input. – Dan Roberts Sep 24 '22 at 03:16
  • I'll take a look tomorrow if you still need help. Good luck – Dan Roberts Sep 24 '22 at 03:22
  • Thank you for that, and i'm sorry if I am seeming a bit rude, I am only new and don't know how to actually express what is needed. But so far it is helping thank you – Yonko_S Sep 24 '22 at 04:56