相当于条件中的 GOTO,Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4438516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:44:14  来源:igfitidea点击:

Equivalent to GOTO in conditions, Python

pythonconditional-statementsgoto

提问by Maks

Since there is no goto operator in Python, what technique can be used instead?

Python中没有goto操作符,可以用什么技术代替?

Condition If it is true, go to thread 1, if is false, go to thread 2 In thread we do something small and after that we go to thread 2 where all other actions take place.

条件 如果为真,则转到线程 1,如果为假,则转到线程 2 在线程中,我们做一些小事,然后我们转到线程 2,在那里进行所有其他操作。

回答by st0le

To the best of my knowledge it's not present (thankfully), but you should check this link

据我所知,它不存在(谢天谢地),但你应该检查这个链接

The "goto" module was an April Fool's joke, published on 1st April 2004. Yes, it works, but it's a joke nevertheless. Please don't use it in real code!

“goto”模块是一个愚人节玩笑,发布于 2004 年 4 月 1 日。是的,它有效,但它仍然是一个玩笑。请不要在实际代码中使用它!

回答by Optimal Cynic

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'm assuming that by "thread" you mean a piece of code (otherwise known as a subroutine or a function). If you're talking about threads as in parallel execution then you'll need more detail in the question.

编辑:我假设“线程”是指一段代码(也称为子例程或函数)。如果您在讨论并行执行中的线程,那么您需要在问题中提供更多详细信息。

回答by Martin Kosek

Python is designed to support good coding practicesand GOTO is not one of them. It may lead to unreadable program logic, if not used properly.

Python 旨在支持良好的编码实践,而 GOTO 不是其中之一。如果使用不当,可能会导致程序逻辑不可读。

I suggest to learn code your program in a Python way, do not stick with (sometimes bad) habits from other programming languages. See Python documentation, real mature Python programs and learn.

我建议以Python 的方式学习你的程序代码,不要坚持其他编程语言的(有时是坏的)习惯。查看 Python 文档,真正成熟的 Python 程序并学习。

回答by detly

Since there is no goto operator in Python, what technique can be used instead?

Python中没有goto操作符,可以用什么技术代替?

Constructing your code logically and semantically.

从逻辑和语义上构建您的代码。

if condition:
    perform_some_action()

perform_other_actions()

回答by Jatin Falak

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.

这是我每次在 Python 3.x 中使用的。