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])
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.