Home > other >  Why are my four "final" variable undefined? I thought I defined them before I used them
Why are my four "final" variable undefined? I thought I defined them before I used them

Time:01-08

Before you begin crying of laughter, understand that I started python around 2 weeks ago, and this is something I'm working on just to experiment. Can someone explain to me why my "final" variables are undefined, when I have them listed in the if statements above? In the print statement at the end, it highlights those variables with "final" in them, and tells me that the variables are undefined, and it throws an error when I run it. (there is a function called "diff" above the return statement)

        return x-y

    if sale_type.upper() == "RS":
        final1_quantity = diff(int(rs_quantity), int(sale_quantity))
    elif sale_type.upper() == "BS":
        final2_quantity = diff(int(bs_quantity), int(sale_quantity))
    elif sale_type.upper() == "BP":
        final3_quantity = diff(int(bp_quantity), int(sale_quantity))
    elif sale_type.upper() == "GP":
        final4_quantity = diff(int(gp_quantity), int(sale_quantity))
        
    quant = input("Would you like to see our stock? (Y/N)")
    if quant.upper() == "Y":
        print(f'''{final1_quantity} red shirts.
                {final2_quantity} blue shirts
                {final3_quantity} black pants
                {final4_quantity} grey pants''')```

CodePudding user response:

When it cannot enter the 'if' defined by the variable, the variable is undefined. You must define all of them as

final1_quantity = 0
final2_quantity = 0
final3_quantity = 0
final4_quantity = 0

before the 'if' conditions.

  •  Tags:  
  • Related