Making a Purchase


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


Your code did not run to end. Check the console window for errors.


I expect that code will run properly as everything seems to be done right.


shopping_list = ["banana", "orange", "apple"]
  
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food
    total = total + prices[item]
    return total
    
 print compute_bill(shopping_list)


Stuck on 11/13 "Making a Purchase"
#2

You need a colon at the end of your for loop statement.

for item in food:

You will also need to indent total = total + prices[item] so that it is part of the for loop.

(Note: there is no need to indent return total, because you are running this after the for loop finishes and has the complete total)

In Python 3 the print statement has been replaced with the print function.
So, you need to use print like a function.

print(compute_bill(shopping_list))

Unless this exercise is using Python >3. In that case you should be fine :slight_smile: .

Here is the complete fix that I've described above.

def compute_bill(food):
    total = 0
    for item in food:
      total = total + prices[item]
    return total
    
print(compute_bill(shopping_list))

#3

Thank you very much!! :grin:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.