GOTO equivalent in terms of Python

Since Python doesn't have a goto statement, which method can I use instead?

State If this is true, go to thread 1, if it is false, go to stream 2. In the stream we do something small, and then go to stream 2, where all other actions take place.

+5
source share
5 answers

Since Python doesn't have a goto statement, which method can I use instead?

Building logical and semantic code.

if condition:
    perform_some_action()

perform_other_actions()
+14
source
def thread_1():
  # Do thread_1 type stuff here.

def thread_2():
  # Do thread_2 type stuff here.

if condition:
    thread_1()

# If condition was false, just run thread_2(). 
# If it was true then thread_1() will return to this point.
thread_2()

edit: I assume that by "thread" you mean a piece of code (otherwise known as a subroutine or function). If you are talking about threads, as in parallel execution, you will need more details in the question.

+6
source

, ( ),

"goto" , 1 , , . , !

+5

Python is designed to support good coding techniques, and GOTO is not one of them. This can lead to unreadable program logic if it is not used properly.

I suggest learning your program code using Python ; don't stick to (sometimes bad) habits from other programming languages. See Python documentation, real mature Python programs and find out.

+2
source
def thread1():
    #write your thread 1 code here

    print("entered no is 1")

def thread2():
    #write your thread 2 code here
    print("Number is greater or less then one.")

def main():
   a=input()
   if a==1:
   thread1()
   elif a<=1 or a>=1:
   thread2()
    #you can use recursion here in case if you want to use agin and again
    #if you want to print serveral time you can use looping.
    for i in range(4):
        main()
    #if you want to run goto forever and ever and ever then remove loop in 
    #this code.

#this code will enable you the equivalent of goto statement.

This is what I use every time in Python 3.x.

0
source

All Articles