2017-10-07 20 views
-1

私は、コードのどこかに戻るようにPythonに指示する行をコード化する方法があるのだろうか?このようなPython:プログラムに戻るように指示する方法はありますか?

何か:そのような本当に基本的な

choose = int(input()) 
if choose == 1: 
    print(“Hi.”) 
else: 
*replay line1* 

何か?

特に大きなループを使用する必要はありませんが、可能な場合は可能ですか?

すべてのアイデアは、私は本当にPythonの新機能ですか?

+4

ループを使用する必要があります。 – Li357

+2

探している用語は_control flow statement_です。そして、はい、Pythonにはいくつかあります。 @AndrewLiが既に言ったように、これを達成するためにループを使うことができます。 –

+0

基本的に、最近のプログラミング言語では広く使われていない制御構造、つまりGOTO文を探しています。これは[構造化プログラミング](https://en.wikipedia.org/wiki/Structured_programming)の出現のためです。これにはループを使用する必要があります。 –

答えて

2
choose = 0 
while (choose != 1) 
    choose = int(input()) 
    if choose == 1: 
     print(“Hi.”) 
0

これは奇妙な一方のビットであり、それは、値が(2つだけの期待値)をブールすると予想される場合に適している、それらのブール値が0または1のいずれかであり、そしていくつかの他の任意のありません文字列、aaand入力を保存したくない場所。

while int(input()) != 1: 
    # <logic for else> 
    pass # only put this if there's no logic for the else. 

print("Hi!") 

のような代替方法があるけれども:

choose = int(input()) 
while choose != 1: 
    <logic for else> 
    choose = int(input()) 

それとも機能を作成することができます。これ

def poll_input(string, expect, map_fn=str): 
    """ 
    Expect := list/tuple of comparable objects 
    map_fn := Function to map to input to make checks equal 
    """ 

    if isinstance(expect, str): 
     expect = (expect,) 

    initial = map_fn(input(string)) 
    while initial not in expect: 
     initial = map_fn(input(string)) 

    return initial 

そして、そのように使用します。

print("You picked %d!" % poll_input("choice ", (1, 2, 3), int)) 

は、より曖昧な場合

関連する問題