2016-03-25 29 views
0

私は "0"を入力したときに私のプログラムが終了しない理由を知ろうとしています。メニューをもう一度やり直すだけです。Pythonプログラムが終了しません

def menu(): 
    print('\n\n\n\n') 
    print('List Processing Program Menu') 
    print('0 to exit') 
    print('1 to view data') 
    print('2 to append data') 

    while(1): 
     try: 
      choice = -1 
      while(choice < 0 or choice > 2): 
       choice = int(input('Please enter a valid number choice ')) 
      break 
     except ValueError: 
      print('Enter an integer number for your menu selection') 
     return choice 

def main(): 
    while(1): 
     choice = menu() 
     if(choice == 0): 
      break 
main() 
+4

あなたの 'return'ステートメントは、あなたがそれを得る前に'中断する 'whileループの中にあるので、 'menu'から値が返されません。したがって、 'main'の内部では、常に' None'が選択されます。 'return'ステートメントをインデントしないと、これは期待どおりに動作します。 – Suever

答えて

0

while (choice < 0 or choice > 2):ループの後、あなたはbreakを言うが、return choicewhile(1)ループの内側にあります。つまり、暗黙のうちにNoneが返され、choiceではありません。 return choice行のインデントを解除するだけです。

1
while(choice < 0 or choice > 2): 
    choice = int(input('Please enter a valid number choice ')) 
    break 

ここに問題があります。 whileループが壊れて、常にNoneが返されます(すべてのPython関数の最後にNoneの暗黙のreturnがあります)。

次のようにあなたがあなたのコードをクリーンアップすることができます:あなたのreturn文は、エントリが有効であるときのうち、あなたがbreakあなたwhileループ、内側にあるので

def menu(): 
    print('\n\n\n\n') 
    print('List Processing Program Menu') 
    print('0 to exit') 
    print('1 to view data') 
    print('2 to append data') 

    choice = None 
    while choice not in [0, 1, 2]: 
     try: 
      choice = int(input('Please enter a valid number choice ')) 
     except ValueError: 
      print('Enter an integer number for your menu selection') 
    return choice 
0

あなたmenu()機能はNoneを返します。

インデント解除あなたreturnの文だけreturn代わりbreakを使用してのループ内から、まだwhile、またはより良いとラインアップそれはそう。

0

最初のwhileループ内にあるので、choiceを返すことはありません。ループ中に抜け出すと、特定の戻り値が指定されていないので、Noneを返すだけです。ここに2つのオプションがあります。

は、whileループの外でリターンを移動するか、このような何かを:

try: 
     choice = -1 
     while(choice < 0 or choice > 2): 
      return int(input('Please enter a valid number choice ')) 
    except ValueError: 
     print('Enter an integer number for your menu selection') 
1

あなたreturn文はそのwhileループの内側にあるため、値がこれまでmenuから返されませんあなたの前にあなたがbreakあなたにそれを得る。したがって、mainの中では、選択肢は常にNoneです。

returnステートメントをインデントしないと、これは期待どおりに機能します。

def menu(): 
    print('\n\n\n\n') 
    print('List Processing Program Menu') 
    print('0 to exit') 
    print('1 to view data') 
    print('2 to append data') 

    while(1): 
     try: 
      choice = -1 
      while(choice < 0 or choice > 2): 
       choice = int(input('Please enter a valid number choice ')) 
      break 
     except ValueError: 
      print('Enter an integer number for your menu selection') 

    return choice 

def main(): 
    while(1): 
     choice = menu() 
     if(choice == 0): 
      break 
main() 

あなたが簡潔になりたい場合は、あなたが実際にとにかくそれらを実行する必要はありませんので、あなたはあなたのtry/except文のすべてを削除することができます。

def menu(): 
    choice = -1 
    while choice < 0 or choice > 2: 
     choice = int(input('Please enter a valid number choice')) 

    return choice 


def main(): 
    choice = -1 
    while choice != 0: 
     choice = menu()  
0

選択を読み込んだときに改行をインデントする必要があります。そうしないと、メニュー機能は常に[なし]を返します。

try: 
     choice = -1 
     while(choice < 0 or choice > 2): 
      choice = int(input('Please enter a valid number choice ')) 
      break 
    except ValueError: 
     print('Enter an integer number for your menu selection') 
関連する問題