2016-05-02 21 views
0

gotoのインストールに問題がありますが、コード内で使用したいのかどうかは分かりません。メインループの内側のどこかで条件付きでポイントする、どうすればそれをPythonで理解できますか?ここ無限ループ内の特定のポイントに戻る方法

は、私はあなたがwhile Trueプラスcontinuebreakとその動作を得ることができます後藤

x = 0 

#some code 

a + b # just some place in code, which must be return point if further condition is true 

if x > 0: # if x is not 0 make it 0 
    x = (x == 0) 

#some code 

if y == 1: # some result to make further condition true 
    x = (x + 1) 

if x == 1:   
    # and here if I have this condition from above go to a + b and start from there 

#some code 
+0

で、私はあなたが求めているのかわからないんだけど。しかし、標準のPythonには 'goto'はありません。あなたは 'turtle.goto'を意味しましたか?あるいは、 '' goto''(http://entrian.com/goto/)モジュールでしょうか? –

+0

はいgotoモジュールですが、インストールに問題があります。何とかgotoを使わずに投稿を編集しました。 –

答えて

2

せずにそれを得ることができる場合はtrueを無限ループといいながら、内部のである構造です。モジュールは必要ありません。

x = 0 

#some code 

while True: 

    a + b # just some place in code, which must be return point if further condition is true 

    if x > 0: # if x is not 0 make it 0 
     x = (x == 0) 

    #some code 

    if y == 1: # some result to make further condition true 
     x = (x + 1) 

    if x == 1:   
     # and here if I have this condition from above go to a + b and start from there 
     continue 
    break 

#some code 

EDIT:機能

def other_function(): 
    a = 1 
    while True: 
     # some code 
     if a == b: 
      continue 
     # some more code 
     if sin(a) < sqrt(a**2 + b**2): 
      break: 
     # more code 
    # still more code 

def main(): 
    # some code 
    while True: 
     # some more code 
     other_function() 
     # still more code 
    # after a while 
    exit(0) 
+0

こんにちは。すべてがメインの中にあり、真の無限ループですので、コードの選択された部分がブレークして正確にこのように動作します。より正確にはブロック間で行われますが、特定の場所ではこのように使用するのが難しいです –

+0

次に、コードを構造化するために別々の関数に分割します。ループの残りの部分をif節の中に入れ、goto-conditionが真でない場合にのみ実行することもできます。 –

+0

あなたは私にこのケースのための関数を使って簡単な例を見せてもらえますか? –

関連する問題