2016-11-05 7 views
0

Python switch文を試してみました。このループを働かせてください。毎回同じことを印刷するだけです。Python madlib whileループの問題

choice = input("Do you want to play a game? (y) or (n)") 
while choice == "y": 
while True: 
    print("1. Fun story") 
    print("2. Super Fun story") 
    print("3. Kinda Fun story") 
    print("4. Awesome Fun story") 
    print("5. Some Fun story") 

    choice2 = int(input("Which template of madlib would you like to play(Enter the number of your choice")) 

if choice2 == 1: 
    noun1 = input("Enter a noun: ") 
    plural_noun = input("Enter a plural noun: ") 
    noun2 = input("Enter another noun: ") 
    print("Be kind to your {}-footed {}, or a duck may be somebody’s {}".format(noun1, plural_noun, noun2)) 


else: 
    print("Goodbye") 
+0

インデントを修正してください。 –

答えて

0

「真」を使用すると非常に簡単に問題を作成できます。適切な終了条件でプログラムを終了したいかもしれませんが、インデントとブレークステートメントのいくつかの調整は問題を解決します。以前は、if条件が2番目のwhileループの外側にあるため、決して到達しませんでした。これにより、選択肢2が作成された後にストーリーの選択肢が再び印刷されます。これは動作するはずです:

choice = input("Do you want to play a game? (y) or (n)") 
while choice == "y": 
    while True: 
     print("1. Fun story") 
     print("2. Super Fun story") 
     print("3. Kinda Fun story") 
     print("4. Awesome Fun story") 
     print("5. Some Fun story") 

     choice2 = int(input("Which template of madlib would you like to play (Enter the number of your choice) ")) 
     break # break out of this while loop to reach if/else 

    if choice2 == 1: 
     noun1 = input("Enter a noun: ") 
     plural_noun = input("Enter a plural noun: ") 
     noun2 = input("Enter another noun: ") 
     print("Be kind to your {}-footed {}, for a duck may be somebody’s {}".format(noun1, plural_noun, noun2)) 

    else: 
     choice = "n" # Assume user does not want to play, reassign choice to break out of first while loop (exit condition to prevent infinite loop of program) 
     print("Goodbye")