首页 > 解决方案 > 如何在另一个函数中调用一个函数进行计算?

问题描述

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()

输入:Enter Supplier name: tab

The id of the part: abs01

How many orders from supplier: 100

输出:NameError: name 'warehouse_initial_quantity_aircondSec' is not defined

如何在第一个函数中添加值warehouse_initial_quantity_aircondSec和第二个函数中的warehouse_initial_quantity_aircondSec

新手,见谅><

标签: pythonpython-3.xlist

解决方案


您试图在第二个函数中使用一个变量,该变量在第一个函数中定义为局部变量,您应该返回该值以使用它:

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()  

推荐阅读