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.
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)
Hi Adnan!
Thanks for reaching out.
You just need to add the following line to the beginning of your phrase.
Best,
Martin
money_spent = 0In 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