2016-08-04 12 views
0

ユーザーがプログラムの最後に停止できるループを作成しようとしています。私はいろいろなソリューションを試しましたが、どれもうまくいきませんでした。私がすることができたのはループを作成することでしたが、私はそれを終わらせることはできません。私は最近、Pythonの学習を始めました。誰かがこの問題について私に啓発できるのであれば、私は感謝しています。無限ループ問題(Python)の終了

def main(): 
while True: 
    NoChild = int(0) 
    NoAdult = int(0) 
    NoDays = int(0) 
    AdultCost = int(0) 
    ChildCost = int(0) 
    FinalCost = int(0) 


    print ("Welcome to Superslides!") 
    print ("The theme park with the biggest water slide in Europe.") 

    NoAdult = int(raw_input("How many adults are there?")) 
    NoChild = int(raw_input("How many children are there?")) 
    NoDays = int(raw_input("How many days will you be at the theme park?")) 
    WeekDay = (raw_input("Will you be attending the park on a weekday? (Yes/No)")) 

    if WeekDay == "Yes": 
     AdultCost = NoAdult * 5 

    elif WeekDay == "No": 
     AdultCost = NoAdult * 10 

    ChildCost = NoChild * 5 

    FinalCost = (AdultCost + ChildCost)*NoDays 

    print ("Order Summary") 
    print("Number of Adults: ",NoAdult,"Cost: ",AdultCost) 
    print("Number of Children: ",NoChild,"Cost: ",ChildCost) 
    print("Your final total is:",FinalCost) 
    print("Have a nice day at SuperSlides!") 

    again = raw_input("Would you like to process another customer? (Yes/No)") 
    if again =="No": 
     print("Goodbye!") 
     return 
    elif again =="Yes": 
     print("Next Customer.") 

    else: 
     print("You should enter either Yes or No.") 

if __name__=="__main__": 
main() 
+1

あなたのプログラムはいつ終了するのでしょうか、代わりに何をしていますか?また、Python 3の 'print()'関数の構文をPython 2の 'raw_input()'と一緒に使っていて、いくつかの問題があります。これらの 'var = int(0)'の行は必要ありません。Pythonで変数を初期化する必要はありません。ユーザーが最初の質問に「はい」と「いいえ」のどちらにも答えない場合はどうなりますか? –

+0

breakを使ってみましたが、ループは実行を続けました。 –

+0

戻り値を改行に置き換えます。 – lonewaft

答えて

0

あなたは破るためにリターンを変更することができ、それはwhileループにこれに代え

if again =="No": 
    print("Goodbye!") 
    break 
+0

私はそれが問題だとは分かりません。 – Li357

+1

'return'はすべての機能を終了させ、ループも壊します。 'break'の必要はありません – dashiell

+0

正しいですが、breakを使うと__main__でもっとやることができます –

0

を終了します:

while True: 

あなたは、この使用する必要があります。

again = True 
while again: 
    ... 

    usrIn = raw_input("Would you like to process another customer? y/n") 
    if usrIn == 'y': 
     again = True 
    else 
     again = False 

私はちょうどFalseにデフォルトにしましたが、あなたはc yまたはnを入力しないと、常に新しい入力をユーザーに求めるようにします。

0

あなたのコードをpython 3.5でチェックしましたが、3.538の入力が2.7のraw_inputなのでraw_inputinputに変更した後で動作しました。関数としてprint()を使用しているので、インポートセクションの将来のパッケージからprint関数をインポートする必要があります。スクリプトにインポートセクションが表示されません。

正確には機能しません。

追加:コマンドラインアプリケーションを終了するには、終了と終了の代わりに終了コードを使用します。つまり、あなたのPythonスクリプトのインポートセクションに

import sys 

に持っていると、ユーザーがプログラムを終了するかどうかをチェックするには、これはあなたにエラーの場合の機会を与える

if again == "No": 
    print("Good Bye") 
    sys.exit(0) 

を行うだろう別の終了コードで終了します。

関連する問題