0

I need to skip the data using continue while using for loop

def skip_data(i):
    if i == 5:
       continue
    else:
        print(i)

for i in range(0,10):
    skip_data(i)
Ajay Kk
  • 11
  • 3
  • 3
    change `continue` to `return` – Max Jan 16 '23 at 14:08
  • 3
    @Max There is error showing in continue.. When I used return it works – Ajay Kk Jan 16 '23 at 14:11
  • 1
    While `return` works in this example, it's not really the same as putting code in the loop that does a `continue`. You'd see that if there was more in the loop than the one function call, the other stuff would still run. There's not really any way for a function's code to interact with a loop happening outside of itself. That's because the function doesn't know it's being called from inside a loop. It could be called in the loop sometimes, and from outside other times. In the latter case, a `continue` statement wouldn't make any sense. – Blckknght Jan 18 '23 at 07:20
  • Does this answer your question? [break and continue in function](https://stackoverflow.com/questions/13986884/break-and-continue-in-function) – Tomerikoo Jan 29 '23 at 09:51

1 Answers1

0

continue is a keyword used inside loops. Since, you have not used it inside a loop it gives you an error.

You can either just use return instead of continue or if you know specific data that you need to skip for example in this case you need to skip 5 you can do something like this :-

def skip_data(i):
    if i != 5:
       print(i)

for i in range(0,10):
    skip_data(i)
Nehal Birla
  • 142
  • 1
  • 14