Question

I'm working with the below dictionaries and list: stock = {"banana": 6, "apple": 0, "orange": 32,...

I'm working with the below dictionaries and list:

stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}

groceries = [‘apple’, ‘banana’, ‘pear’]

I need to write code to sum the total number in stock of the items in the groceries list. The second part is to write code to print the total value in stock of all items. The program can iterate over the stock dictionary and for each item multiply the number in stock times the price of that item in the prices dictionary. I have tried a few different ways. I have this, but it doesn't work. groceries = ['apple', 'banana', 'pear']

def sum_list(groceries):

sum_numbers = 0

for x in groceries:

sum_numbers += x

return sum_numbers

Homework Answers

Answer #1
def sum_list(groceries, stock, prices):
    sum_numbers = 0
    for x in groceries:
        if x in stock:
            sum_numbers += stock[x] * prices[x]
    return sum_numbers


# Testing the function here. ignore/remove the code below if not required
stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
groceries = ['apple', 'banana', 'pear']
print(sum_list(groceries, stock, prices))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
I'm working on the below problem: For this question, you are to write a program that...
I'm working on the below problem: For this question, you are to write a program that reads the data in the file state_satscores_2004.txt. Each line of this file has name of a state, mean Verbal SAT score, and mean Math SAT score. After reading the data, a. Print the state with the highest mean Verbal SAT score b. Print each state that has a mean Math SAT score greater than 500 Submit the code and the output from your program....
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT