English 中文(简体)
有条件到达GTO
原标题:Equivalent to GOTO in conditions, Python
  • 时间:2010-12-14 11:11:10
  •  标签:
  • python
  • goto

由于在沙尔没有 go子经营者,可以采用什么技术?

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.

问题回答

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

顺理成章地制定你的法典。

if condition:
    perform_some_action()

perform_other_actions()
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.

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

我建议在<>上学习你的节目。 灰色通道没有与其他节目语言的习惯(有时是坏)。 参看Adhury文件、真正的成熟的Sharma方案和学习。

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.

这就是我每次在3.x中使用的东西。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签