Home > OS >  "Return outside function" error for 'for' loop in python
"Return outside function" error for 'for' loop in python

Time:07-25

The following code returns me an error in python saying return outside function while using for loop. Can someone please let me know where i'm doing it wrong. I'm new to python.


strvar=["A","B", "C", "D", "E", "F"]
for i in strvar:
    print("the perc in "   i)
    return i 1

CodePudding user response:

All that means is that your return statement is unnecessary since you are not inside of a function.

Also since you are iterating over the values of the string there is no need to increment i since i is not an integer.

instead change your code to something like this:

strvar=["A", "B", "C", "D", "E", "F"]
for i in strvar:
    print("the perc in "   i)
  • Related