-4

In a python code, how to skip a few lines of code?

For example, this is the code:

if value == 10:
    <skip_the_next_seven_lines_of_code>
if value == 15:
    <skip_the_next_eight_lines_of_code>
#code continues...

Note that this can be done using a lot of workarounds, like putting the code in the else statement, but I want to avoid that as it would require a lot of elses as I have a lot of ifs.
I am asking whether there is a function which is like:
skiplines(7)
Then the next 7 lines are skipped.

Also, this question has been downvoted many times, but I do not understand why. If there is no such way to accomplish this it can be answered in that way...

CoolCoder
  • 786
  • 7
  • 20
  • What do you mean by *skip a number of lines*? Using ```if...else```, you can skip a lot of lines –  Aug 02 '21 at 02:01
  • I have edited the question. Note that I want to specifically avoid that – CoolCoder Aug 02 '21 at 02:03
  • So use `if value != 10:` and put the lines there. Then it won’t be in an else – Mark Tolonen Aug 02 '21 at 02:04
  • Yes :). But this does not answer my question, as it does not `skip` lines...it is also just a workaround... – CoolCoder Aug 02 '21 at 02:05
  • 1
    That’s exactly what it does. You make no sense – Mark Tolonen Aug 02 '21 at 02:06
  • In that case, you should use classes and functions –  Aug 02 '21 at 02:10
  • 1
    @Sujay, can you provide an example? – CoolCoder Aug 02 '21 at 02:12
  • _"Note that this can be done using a lot of workarounds, like putting the seven-lines-of-code in the else statement, but I want to avoid that."_ `if-else` is not a workaround. Why do you want to avoid *standard* Python syntax? – AGN Gazer Aug 02 '21 at 02:12
  • ```if...else```, all statements indented in it will be skipped. That is why they are called **control statements** –  Aug 02 '21 at 02:14
  • 1
    Is there a language (besides possibly Assembly; but even there it is in bytes - not lines, I think) that supports these `skip`s? – AGN Gazer Aug 02 '21 at 02:20
  • @AGN Lauguages with Go-to syntax would support that. – Kota Mori Aug 02 '21 at 02:27
  • So, are you asking whether Python has `goto`? And why do you want to avoid `if-else`? – AGN Gazer Aug 02 '21 at 02:28
  • 3
    Sounds like a `goto`. Someone implemented [`goto` in Python](https://github.com/cdjc/goto). – Michael Ruth Aug 02 '21 at 02:32
  • @AGNGazer that is what I am asking. I want to avoid `if-else` because that would make a lot of repetitive `if-else`s if there are a lot of `if`s and then to make it less repeatitive, we would need nested `if-else`s which would make the code even complex to read (my opinion) – CoolCoder Aug 02 '21 at 02:34
  • @CoolCoder BTW, I agree that this question does not have to be downvoted; Maybe it is not standard syntax on Python, but this does not necessarily mean the question is bad. Perhaps people are confused why "if-else" is seen as a workaround and to be avoided. You could add an example with a lot of nested "if-else" and ask for approaches to simplify and improve the readability of the code. – Kota Mori Aug 02 '21 at 02:38
  • I can't imagine using `goto` to _"improve the readability"_. – AGN Gazer Aug 02 '21 at 02:41
  • 1
    I downvoted because the problem to solve is not clear. If OP has messy code, show the messy code and ask if there is a better way. And then the code probably belongs on code review if it is working code that needs to be improved. – Mark Tolonen Aug 02 '21 at 02:57
  • _"If there is no such way to accomplish this it can be answered in that way..."_ There is _a way to accomplish_ (or _achieve_) but you do not like it - see your comment to my answer. – AGN Gazer Aug 02 '21 at 03:03

2 Answers2

1

I don't think you can handle by the number of lines. A closest I could think of is to define a function of the corresponding code block as below.

value = 10  # or set other value

task = lambda: print("this is original task")

if value == 10:
    task = lambda: None

task()  # if value==10, this does nothing.
Kota Mori
  • 6,510
  • 1
  • 21
  • 25
0

This is the original answer to the original question (see question edits):

if value != 10:
    <some_seven_lines>
<the_rest_of_the_code>

The updated question shows a slightly more complicated pattern. Let's say something like this (I have to create an example since OP does not provide specific code/problem that he/she is trying to solve):

    a = 0  # some variable that lines of code will modify
    if value == 10:
        goto L1  # same as "skip next 7 lines"
    if value == 15:  # line 1 to "skip"
        goto L2      # line 2 to "skip"; same as "skip next 8 lines"
    a += 1           # line 3 (or 1 from second "if") to "skip"
    a += 10          # line 4 (or 2 from second "if") to "skip"
    a += 100         # line 5 (or 3 from second "if") to "skip"
    a += 1000        # line 6 (or 4 from second "if") to "skip"
    a += 10000       # line 7 (or 5 from second "if") to "skip"
L1: a += 100000      # line - (or 6 from second "if") to "skip"
    a += 1000000     # line - (or 7 from second "if") to "skip"
    a += 10000000    # line - (or 8 from second "if") to "skip"
L2: a += 100000000   # line 11 (or 13 from second "if")

This could be done in the following way:

a = 0

def f1(a):
    a += 1
    a += 10
    a += 100
    a += 1000
    a += 10000
    return a

def f2(a):
    a += 100000
    a += 1000000
    a += 10000000
    return a

if value == 10:
    a = f2(a)
elif value == 15:
    pass
else:  # or elif value != 15 and skip the branch above
    a = f1(a)
    a = f2(a)

a += 100000000

Of course, specific answer would depend on the question with a specific example, which, as of now, is missing.

Also, see https://stackoverflow.com/a/41768438/8033585

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
  • This *achieves* the same work I want to achieve...but it does not *skip* lines.. – CoolCoder Aug 02 '21 at 02:06
  • @AGN can you please add some description in your answer. like what you have do, what OP is doing wrong. why OP should follow your answer – imxitiz Aug 02 '21 at 03:18
  • 1
    @Xitiz _"why OP should follow your answer"_ Because it is achieving exactly what OP wanted to achieve, as OP admitted in the comment above: _"This achieves the same work I want to achieve..."_ – AGN Gazer Aug 02 '21 at 03:58