2017-06-17 12 views
0

私はPython v3が初めてで、プログラムの最後にwhileループを使用して、ユーザがプログラムを再起動/再試行したいかどうかを判断します。Whileループでプログラムを終了する

私が「はい」を選択して複数回プログラムを繰り返してからいいえを選択した場合、再試行した回数は「もう一度検索しますか(Y/N)」オプションが表示されますプログラム例3回の試合が行われ、休憩が効くまでに3回n回進入しなければならない。

コードは以下のとおりです。

while True: 
    finish_input = input("Would you like to search again: (Y/N) > ")    
    if finish_input.lower() == ("y"): 
     my_project()#restarts the program from the start 
     continue 
    elif finish_input.lower() == "n": 
     print() 
     print("Thank you for using this service.") 
     break 
    else: 
     print() 
     print("Invalid entry. Please enter Y or N") 

再起動するオプションが必要ですが、nを1回入力するだけでプログラムを終了/中断して終了できます。ヘルプは本当に感謝しています。

答えて

0

何がしたいことは次のとおりです。

def my_project(): 
    #Your other code here 
my_project() 
#The code that you posted 

しかし、あなたがやっている:

def my_project(): 
    #Your other code here 
    #The code that you posted 

を差が最後の一つに、あなたがプログラム内でループしていることである:各yです後で、それぞれの関数の呼び出しには、nを置く必要があります。

コードは次のようになります

def my_project(): 
    #Your other code here 
my_project() 

while True: 
    finish_input = input("Would you like to search again: (Y/N) > ") 
    if finish_input.lower() == "y": my_project() 
    elif finish_input.lower() == "n": 
     print("\nThank you for using this service.") 
     break 
    else: print("\nInvalid entry. Please enter Y or N") 
+0

あなたの提案は、私の問題を解決しており、ご指導は非常にはっきりと私のような初心者のために分かりやすかったです。 – bagpuss

+0

ありがとう!これが便利だと分かったので、[回答を受け入れてください](https://stackoverflow.com/help/someone-answers) –

0

私はこれを実装するには悪い方法だと思います。このようなことはどうですか?

#program starts 
run_prog = True 

while run_prog: 
    #Your original code 

    finish_input = "a" 
    while True: 
    finish_input = input("Would you like to search again: (Y/N) > ") 
    if finish_input.lower() == ("y"): 
     run_prog = True 
     break 
    elif finish_input.lower() == "n": 
     run_prog = False 
     print() 
     print("Thank you for using this service.") 
     break 
    else: 
     print() 
     print("Invalid entry. Please enter Y or N") 
関連する問題