Concerning Dictionaries and += in Python 2.x

Luke Tomaneng luketomaneng at gmail.com
Tue Jan 20 01:31:09 CET 2015


On Monday, January 19, 2015 at 4:21:58 PM UTC-8, Chris Angelico wrote:
> On Tue, Jan 20, 2015 at 11:12 AM, Luke Tomaneng 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

Hm. I fixed the indentation like you said, and it worked fine. The only reason I changed the indentation to what you saw in the first place is because Codecademy's Python engine registered an error. However, repl.it accepted the script. I have concluded that the original mistake was actually on the Codecademy site instead of my script. Thanks for helping me out.



More information about the Python-list mailing list