Python Forum

Full Version: Evaluating multiple lists in a tuple
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
we have a program that evaluates and adds the content of one list but we want a function that evaluates and adds for an arbitrary amount of lists. right now, if I input cost_calculator(["olive","ham"]), this would return the price which is 15.00

What I want is for me to input cost_calculator(["olive","ham"],["mushroom"]) and give me the price of two pizzas with those toppings which is 28.50

Any ideas of what I am doing wrong or need to put in?

def cost_calculator(in_list):
    cost_pizza = 0
    cost_pizza += 13.00
    pepperoni = ["pepperoni"]
    mushroom =["mushroom"]
    olive = ["olive"] 
    anchovy = ["anchovy"]
    ham = ["ham"]
    for item in in_list:
        if item in pepperoni:
            cost_pizza += 1.00
        if item in mushroom:
            cost_pizza += .50
        if item in olive:
            cost_pizza += .50
        if item in anchovy:  
            cost_pizza += 2.00
        if item in ham:
            cost_pizza += 1.5
    return cost_pizza
One of the things a function is used for is to call it multiple times with different parameters. Can't help any further because you did not post how you are calling the function, and I do not want to guess at code that is not indented.
Create another function that calls cost_calculator with each pizza list and adds them up.
def cost_calculator(in_list):
    cost_pizza = 0
    cost_pizza += 13.00
    pepperoni = ["pepperoni"]
    mushroom = ["mushroom"]
    olive = ["olive"] 
    anchovy = ["anchovy"]
    ham = ["ham"]
    for item in in_list:
        if item in pepperoni:
            cost_pizza += 1.00
        if item in mushroom:
            cost_pizza += .50
        if item in olive:
            cost_pizza += .50
        if item in anchovy:  
            cost_pizza += 2.00
        if item in ham:
            cost_pizza += 1.5
    return cost_pizza


def pizzas_cost(*pizzas):
    cost = 0
    for pizza in pizzas:
        cost += cost_calculator(pizza)
    return cost


print(pizzas_cost(["olive", "ham"], ["mushroom"]))
Output:
28.5