The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Iterating over dictionary

Iterating over dictionary

0
Votes
1
Answer

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 Answer

365 Team
0
Votes

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

×
Ready to Learn
Data Science?
50% OFF