首页 > 解决方案 > 子程序后变量不保存

问题描述

我有一个学校项目来创建一个交互式菜单,允许理论上的餐厅员工输入客户想要的项目并输出表格和成本(第一个数字是桌子,第二个及以上是菜单项)

但是由于某种原因,在 process() 中,成本只是输出为 0,即使在调试中您可以看到它正在处理成本。

## -- Setup -- ##
Table = 0 # Used to display what table an order comes from
Menu = ["All day (Large)", "All day (Small)", "Hot Dog", "Burger", "Cheese burger", "Chicken goujons", "Fries", "Salad", "Milkshake",
    "Soft drinks", "Still Water", "Sparkling water"] # Menu Items, if statements will be used for the prices
MenuPrice = ["5.50", "3.50", "3.00", "4.00", "4.25", "3.50", "1.75","2.20", "1.30", "0.90", "0.90"]

## -- Main -- ##
# Order Input
i = input("Enter Table and Order number (Seperate with comma): ") # Order Input

# Order Processing
Order = i.split(",") # Splits the list where there is ","
Orderlen = len(Order)
Table = Order[0] # This is for displaying what table the order will go on
Table = int(Table) # Converting Table from a str to an int

global ii, Food, Cost, CostAdd
ii = 0 # Variable to complete a liner search through the order list
Food = [] # For displaying the food
Cost = 0 # For displaying the cost
CostAdd = 0 # Adds to Cost

def Process(ii, Cost, CostAdd): # Compiles the Order
    ii = ii + 1
    if ii == Orderlen:
        return Cost
        pass
    else:
        Pointer = Order[ii]
        Pointer = int(Pointer)
        Pointer = Pointer - 1 # Syncs input with the Diners Menu
        Food.append(Menu[Pointer])
        CostAdd = MenuPrice[Pointer]
        CostAdd = float(CostAdd)
        Cost = Cost + CostAdd
        print(f"Cost: {Cost}, CostAdd: {CostAdd}")

        Process(ii, Cost, CostAdd)
Process(ii, Cost, CostAdd)

# Order Output
print(f"NEW  Cost: {Cost}, CostAdd: {CostAdd}")
print(f"{Food} for Table: {Table}") # Displays what food is going to what table
print(f"This will cost: {Cost}")

任何帮助将不胜感激,在此先感谢!

标签: python-3.x

解决方案


我相信这会给你带来你正在寻找的输出,或者至少会帮助你朝着正确的方向前进:

Table = 0 # Used to display what table an order comes from
Menu = ["All day (Large)", "All day (Small)", "Hot Dog", "Burger", "Cheese burger", "Chicken goujons", "Fries", "Salad", "Milkshake",
    "Soft drinks", "Still Water", "Sparkling water"] # Menu Items, if statements will be used for the prices
MenuPrice = ["5.50", "3.50", "3.00", "4.00", "4.25", "3.50", "1.75","2.20", "1.30", "0.90", "0.90"]

## -- Main -- ##
# Order Input
i = input("Enter Table and Order number (Seperate with comma): ") # Order Input

# Order Processing
Order = i.split(",") # Splits the list where there is ","
Orderlen = len(Order)
Table = Order[0] # This is for displaying what table the order will go on
Table = int(Table) # Converting Table from a str to an int

global ii, Food, Cost, CostAdd
ii = 0 # Variable to complete a liner search through the order list
Food = [] # For displaying the food
Cost = 0 # For displaying the cost
CostAdd = 0 # Adds to Cost

def Process(ii, Cost, CostAdd): # Compiles the Order
    while 1:
        ii = ii + 1
        if ii == Orderlen:
            return Cost,CostAdd
        else:
            Pointer = Order[ii]
            Pointer = int(Pointer)
            Pointer = Pointer - 1 # Syncs input with the Diners Menu
            Food.append(Menu[Pointer])
            CostAdd = MenuPrice[Pointer]
            CostAdd = float(CostAdd)
            Cost = CostAdd
            print(f"Cost: {Cost}, CostAdd: {CostAdd}")

Cost,CostAdd = Process(ii, Cost, CostAdd)

# Order Output
print(f"NEW  Cost: {Cost}, CostAdd: {CostAdd}")
print(f"{Food} for Table: {Table}") # Displays what food is going to what table
print(f"This will cost: {Cost}")

推荐阅读