Python Forum
Evaluating multiple lists in a tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Evaluating multiple lists in a tuple
#1
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
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,431 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Evaluating arithmetic expression with recursion. muddybucket 3 2,759 Dec-17-2021, 08:31 AM
Last Post: deanhystad
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,756 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Create Dict from multiple Lists with duplicate Keys rhat398 10 3,979 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  Better way to append frames from a video to multiple lists? Balaganesh 0 1,819 May-13-2021, 07:37 AM
Last Post: Balaganesh
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,841 Dec-02-2020, 07:50 AM
Last Post: Gilush
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,725 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Assigning multiple values using tuple sivacg 2 2,224 Aug-06-2020, 10:29 PM
Last Post: perfringo
  Reading Multiple Lists Using SUM function dgrunwal 6 3,282 Jun-03-2020, 08:23 PM
Last Post: dgrunwal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020