How in Python to check the condition and go to a specific array of code in the program if the specified condition is met / not observed.

Maybe there is any module that allows you to put a marker in the code to which the interpreter needs to go, if the condition is met / not met.

I ask you to avoid mana and simple "hints", a la shove it all in if ...

  • So you want a goto for python ? There is a library at github.com/snoack/python-goto , but this is a perversion. - Avernial
  • ten
    And yet, I recommend that you do not look for an analogue of goto, but study normal approaches to building a program. As soon as the program becomes a little more complicated than a primitive linear script and there is a need to move from one part of the code to another, you need to begin to structure the code using functions. If you have functions, you can conditionally call one of them, and this is no worse than using the transition to a label, or better to very many. It may be unusual at first, but if you want to program, and not just write short linear scripts, you need to use the right approach. - Xander
  • one
    Give a specific example, you will be prompted solution. Alexander is right. - Vladimir Obrizan September
  • Thanks for the tips and your time guys :) It’s a pity that you can't do this in Python for now. But I think when I will better understand this, everything will turn out. Thanks again and all the good code :) - lavr2004

1 answer 1

 import random a, b = random.randint(1, 2), random.randint(1, 2) class Goto: @staticmethod def label1(): r = a + b print(r) return r @staticmethod def label2(): r = Goto.label1() - 1 print(r) if a > b: Goto.label1() else: Goto.label2() 
  • Unfortunately, these methods will not work very much into a loop between themselves (unlike this goto) - the stack is not enough. - insolor
  • Thank you for your time and support, guys :) I still do not understand Python, but I will understand. If there is no simple solution (function or something else there), then it is easier for me to leave this topic alone and just continue to study further. - lavr2004