0

i am having some trouble trying to follow the code in on-line learning but no response so i figured i'd google and ask here. what i am trying to accomplish is to print each counter and total for the loop to see and follow the logic: please let me know where i can type print total to see the loop in action.

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 x in food:
        total+=prices[x]
    return total
2
  • wow, that was a ridiculously fast edit... i tried putting 'print total' before 'return total' but that didn't work. i tried printing it as a string, but i guess i need help figuring out how to print something within a function. Commented Dec 26, 2013 at 16:38
  • i would like to follow the total as the loop progresses, as well as x. when i print total outside of the function total has not been defined so it doesn't work. when i tried putting total= 0 outside of the function i received an error. Commented Dec 26, 2013 at 16:40

2 Answers 2

2

You can put it right after the for expression:

 for x in food:
   total += prices[x]
   print(total)

make sure your print call is intended the same number of spaces as the total += as indentation is important in python.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. i did this before and it said my total was too high. guess it's a limitation within the gui they provide. curious to see what numbers they were throwing but this definitely helps!
@SYSzero what you mean the numbers were too high? Perhaps food is longer than it should be? Maybe whatever is handing you that iterable has a bug in it.
0

sorry guys, silly question.

reason why it wasn't printing anything was because i never called the function.

after typing print compute_bill(shopping_list) it worked!

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.