1

I am pretty new to Python and am practicing with codeacademy, am getting a strange error message with below function. I dont understand as it looks logically and syntactically correct to me, can anyone see the issue?

def compute_bill(food):
    total = 0
    for item in food:
        total = total + item
    return total

Oops, try again.

compute_bill(['apple']) 

resulted in a

TypeError: unsupported operand type(s) for +: 'int' and 'str'
4
  • 1
    Yes, 0 + 'apple' is not a meaningful operation in Python. You can only add numbers or concatenate strings, not add strings to numbers. Commented Oct 9, 2015 at 9:52
  • are you supposed to pass apple's price ? Commented Oct 9, 2015 at 9:54
  • Hi, yes. I am supposed to pass the price Commented Oct 9, 2015 at 10:02
  • 1
    ok, so your issue is resolved. Is it ? Commented Oct 9, 2015 at 10:27

2 Answers 2

1

You cannot add a string with an integer . typeError on python Docs -typeError

call the function like below-

compute_bill([1]) 
compute_bill([10,20,30]) 

OR

apple = 10
orange = 20
compute_bill([apple,orange]) 
Sign up to request clarification or add additional context in comments.

Comments

1

as @Rilwan said in his answer yo cannot add string with an interger. Since you are working on codeacademy, i have completed similar assignment, I believe you have to get the cost of the food that you send to the function from a dictionary and then calculate the total.

food_cost = { "apples" : 20, "oranges" : 40}
def compute_bill(food):
     total = 0
     for item in food:
         total = total + food_cost[item]
     return total
compute_bill(['apples'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.