Concerning Dictionaries and += in Python 2.x

Chris Angelico rosuav at gmail.com
Tue Jan 20 01:21:37 CET 2015


On Tue, Jan 20, 2015 at 11:12 AM, Luke Tomaneng <luketomaneng at gmail.com> wrote:
> def compute_bill(food):
>     total = 0
>     for item in food:
>         if stock[item] > 0:
>             total += prices[item]
>             stock[item] = stock[item] - 1
>             return total
> Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, but for the same result. If anyone could find the glitch in my code, please let me know. Thanks!
>

In Python, indentation determines block structure. Have another look
at this function, and see if you can figure out where the problem is;
hint: try printing something out every time you claim a piece of
stock.

ChrisA



More information about the Python-list mailing list