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

Error return outside function

Error return outside function

0
Votes
1
Answer

While using the for loop. My code is working when using the print function but not working when using return.
It is showing the error ‘Error outside function’
money_spent = 0
for i in prices:
if prices[i] >= 5:
money_spent = money_spent + (prices[i] * quantity[i])
return (money_spent)
 

1 Answer

365 Team

Hi Srajan!
Thanks for reaching out!
How have you applied indentation to your code? Please make sure you comply with the syntax just as shown in the video (please don’t forget that indentation in Python is crucial!).
Then, return is used when you are defining a function. Here, instead, you are using a loop. We can’t just add the return keyword when it does not regard the creation of a function.
Hope this helps.
Best,
Martin

×
LAST CHANCE
Ready to Learn Data Science?
50% OFF