Python Forum
Evaluating multiple lists in a tuple - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Evaluating multiple lists in a tuple (/thread-17506.html)



Evaluating multiple lists in a tuple - cdogo - Apr-14-2019

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



RE: Evaluating multiple lists in a tuple - woooee - Apr-14-2019

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.


RE: Evaluating multiple lists in a tuple - Yoriz - Apr-14-2019

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