Python madlib while loop issue -


my attempt @ python switch statement basically. cant loop work. prints same thing each time.

choice = input("do want play 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. fun story")      choice2 = int(input("which template of madlib    play(enter number of choice"))  if choice2 == 1:     noun1 = input("enter noun: ")     plural_noun = input("enter plural noun: ")     noun2 = input("enter noun: ")     print("be kind {}-footed {}, or duck may somebody’s {}".format(noun1, plural_noun, noun2))   else:     print("goodbye") 

very easy create problems when using "while true". may want end program proper exit condition, few adjustments indentation , break statement solves problem. before, if conditional never reached, outside of second while loop, causing story choices printed out again after choice2 made. should work:

choice = input("do want play 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. fun story")          choice2 = int(input("which template of madlib play (enter number of choice) "))         break # break out of while loop reach if/else      if choice2 == 1:        noun1 = input("enter noun: ")        plural_noun = input("enter plural noun: ")        noun2 = input("enter noun: ")        print("be kind {}-footed {}, duck may somebody’s {}".format(noun1, plural_noun, noun2))      else:         choice = "n" # assume user not want play, reassign choice break out of first while loop (exit condition prevent infinite loop of program)         print("goodbye") 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -