mr.gnoclue
mr.gnoclue

Reputation: 25

How to call a function in another function for calculations?

def warehouseInventoryCreationBios():
    bios = []
    warehouseName = ["\nWarehouse: WBS"]
    bios.append(warehouseName)
    warehouse_initial_quantity_aircondSec = 1000    
    aircondSec = ["Section: AS", "Parts: compressor", "Part ID: ABS01", "Initial Quantity in the warehouse:", warehouse_initial_quantity_aircondSec, '\n']
    bios.append(aircondSec)
    .
    .
    return bios
warehouseInventoryCreationBios()

def updateBiosWarehouseInventory():
    bios = warehouseInventoryCreationBios()
    warehouseUpdateSupplier = []
    name = input('Enter Supplier name: ')
    name = name.lower()
    id_parts = input('The id of the part: ')
    id_parts = id_parts.upper()
    order_from_supplier = int(input('How many orders from supplier: '))
    warehouseUpdateSupplier.append(name)
    warehouseUpdateSupplier.append(id_parts)
    warehouseUpdateSupplier.append(str(order_from_supplier))
    if name == 'tab':
        if id_parts == "ABS01" or id_parts == "TBS05" or id_parts == "BBS02":
            if id_parts == "ABS01":
                compressor_quantity_warehouse = warehouse_initial_quantity_aircondSec + order_from_supplier
                return compressor_quantity_warehouse
    .
    .
    return warehouseUpdateSupplier
updateBiosWarehouseInventory()

Input: Enter Supplier name: tab

The id of the part: abs01

How many orders from supplier: 100

Output: NameError: name 'warehouse_initial_quantity_aircondSec' is not defined

How can I add the value warehouse_initial_quantity_aircondSec in the first function with the warehouse_initial_quantity_aircondSec in the second function

Newbie here, sorry ><

Upvotes: 0

Views: 56

Answers (1)

K&#225;roly Szab&#243;
K&#225;roly Szab&#243;

Reputation: 1273

You are trying to use a variable in the second function, which is defined in the first as a local variable, you should return that value to use it:

def warehouseInventoryCreationBios():
    bios = []
    warehouseName = ["\nWarehouse: WBS"]
    bios.append(warehouseName)
    warehouse_initial_quantity_aircondSec = 1000    
    aircondSec = ["Section: AS", "Parts: compressor", "Part ID: ABS01", "Initial Quantity in the warehouse:", warehouse_initial_quantity_aircondSec, '\n']
    bios.append(aircondSec)
    .
    .
    # return multiple values (as tuple)
    return bios, warehouse_initial_quantity_aircondSec 
warehouseInventoryCreationBios()  # this line is unnecessary, only calculations with no results used after it

def updateBiosWarehouseInventory():
    # receive multiple values
    bios, warehouse_initial_quantity_aircondSec = warehouseInventoryCreationBios()
    warehouseUpdateSupplier = []
    name = input('Enter Supplier name: ')
    name = name.lower()
    id_parts = input('The id of the part: ')
    id_parts = id_parts.upper()
    order_from_supplier = int(input('How many orders from supplier: '))
    warehouseUpdateSupplier.append(name)
    warehouseUpdateSupplier.append(id_parts)
    warehouseUpdateSupplier.append(str(order_from_supplier))
    if name == 'tab':
        if id_parts == "ABS01" or id_parts == "TBS05" or id_parts == "BBS02":
            if id_parts == "ABS01":
                compressor_quantity_warehouse = warehouse_initial_quantity_aircondSec + order_from_supplier
                return compressor_quantity_warehouse
    .
    .
    return warehouseUpdateSupplier
updateBiosWarehouseInventory()  

Upvotes: 3

Related Questions