Making a Purchase: Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'


#1

Hello! Having issues with another one:

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):
indent_here total=0
indent_here for item in food:
indent_here indent_here total += item
indent_here return total

getting this error: Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'

Is there anybody willing to tell where the issue is, please?

Dont get me wrong, please, but I already have some MEng degree so supposed that I am not that stupid and expected that tutorial tells you something new and then asks you to modify it and use yourself. But this course is more like "you add two numbers like this and now create a scientific calculator". I have literally NO experience with programming and this tutorial is more frustrating than helpful as I usually struggle for a while and then have to come to this forum and copy-paste somebody elses code with some magical paragraph that wasnt even mentioned in the tutorial instructions...


#2

food is a list of items you want to buy. so when you loop over food here:

 for item in food

item can be for example an apple, so then you get:

0 = 0 + "apple"

which gives unsupported operand type for int (total) and str (item)

You need to use item as key to get the price value from the prices dictionary.


#3

Thanks for the reply! Yes, that makes sense. The problem is that I dont know what to do next. : /


#5

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