Last answered:

05 May 2020

Posted on:

30 Apr 2020

0

Iterating over dictionary

Hi team, Every time when i execute below code by 'Shift plus enter' or 'control plus enter' for i in prices:
if prices[i] < 5:
    money_spent = money_spent + (prices[i]*quantity[i])
else:
    money_spent = money_spent
money_spent it gives me answer every time answer by adding 24 to output. I mean first time, when i execute it give me answer 24, which is ok. Then when i again repeat same code in below cell, it gives result 48, by doing third time 72 and so on. My question is how shall i execute the code in Jupyter note book so that every time, i execute it, it gives me result of 24 every time.         
1 answers ( 0 marked as helpful)
Instructor
Posted on:

05 May 2020

0
Hi Adnan! Thanks for reaching out. You just need to add the following line to the beginning of your phrase.
money_spent = 0
In this way, every time you run the loop, you will start from money_spent with a value of 0, and you will not add 24 every time you execute the code cell.  Hope this helps.
Best,
Martin

Submit an answer