Using two variables from two seperate functions in a true false statement without error to create dictionary

Question:

The Code:

def NUMBER(i):
    i = input("enter code: ")
    minimum = 0
    maxiumum = 39 
    if i == "END":
        return False
    elif int(i) > minimum and int(i) <= maximum:
        return i
        return True
    else:
        return False
   
    
def AMOUNT(q):
    q = input("enter quantity: ")
    minimum = 0
    maxiumum = 49
    if int(q) > minimum and int(q):
        return True
    else:
        return False
    

number_result = NUMBER(i)
amount_result = AMOUNT(q)        
    
def shop(i, q):
    number_input = []
    amount_inputed = []
    NUMBER()
    AMOUNT()
    while True:    
        if i != "END":
            if number_result == True and amount_result == True:
                number_input.append(int(i))
                amount_inputed.append(int(q))
            elif number_result == True and amount_result == False:
                print("Invalid Quanity")
                break
            elif number_result == False and amount_result == True:
                print ("Invalid code")
                break 
            else:
                print("Invalid inputs")
                break 
    return number_input,amount_inputed
        

def showRecord(number_input, amount_inputed):
    product_info = {}
    for kk in range(len(number_input)):
        quan = amount_inputed[kk]
        kk = number_input[kk]
        price = priceList[kk] 
        total_price = priceList[kk] * quan
        product = productList[kk]
        if kk not in product_info:
            product_info[kk] = [kk, quan, price, product]
        else:
            product_info[kk][1] += quan
            product_info[kk][2] = product_info[kk][1] * price
            sum_amount += product_info[kk]
            print(sum_amount)
    for x in ["Code", "Quanity", "Price", "Product"]:
        print(x, end="  ")
    print()
    for x in product_info:
        for info in product_info[x]:
            print(info, end="     ")
        print()
    

number_input,amount_inputed= shop(i,q)
showRecord(cod1e,code2)


shop(NUMBER(i), AMOUNT(q))

Hello I am trying to create a validation system for the values i and q across two separate functions (NUMBER and AMOUNT). I want the function to run NUMBER and AMOUNT and if the inputed numbers are valid to keep on repeating the cycle until either the i is "END" or smaller than 0/larger than 39. For q smaller than 0 larger than 49.

Once eithier/both are False or END is inputed i want to gather the inputed values and run shop().

I have tried to both global variables for i and q but it did not work and resorted to defining i and q in each of their functions but the same error occurs.

File "/Users/stev/Documents/shopping_cart.py", line 57, in
number_result = NUMBER(i)
NameError: name ‘i’ is not defined. Did you mean: ‘id’?

I did not know what code to add so I added everything (delete if not allowed I did not know what code was needed).

Asked By: Yonko_S

||

Answers:

I just modified your own code to work properly. although it could be changed totally.

def NUMBER():
    i = input("enter code: ")
    minimum = 0
    maximum = 39 
    
    if (i=="END") or (int(i) > minimum and int(i) <= maximum) :
        return i
    else:
        return False
   
    
def AMOUNT():
    q = input("enter quantity: ")
    minimum = 0
    maximum = 49
    if (int(q) > minimum) and (int(q)<maximum):
        return q
    else:
        return False
    
   
def shop():
    number_input = []
    amount_inputed = []
    
    while True:
        number_result = NUMBER()
        amount_result = AMOUNT()
       
        if number_result=="END":
            print("Canceled by user")
            break
        elif number_result and not(amount_result):
            print("Invalid Quanity")
            break
        elif not(number_result) and amount_result:
            print ("Invalid code")
            break 
        elif not(number_result) and not(amount_result):
            print("invalid input")
            break
        else:
            number_input.append(int(number_result))
            amount_inputed.append(int(amount_result))
    return number_input,amount_inputed
        

number_input,amount_inputed= shop()
print("number_input:",number_input," amount inputed:",amount_inputed)

Have fun 🙂

Answered By: nariman zaeim
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.