Python Forum
How to fix while loop breaking off
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix while loop breaking off
#1
Hello,

So I was trying out a hangman game from this website and I have an issue that I can't seem to solve in other projects as well.

def play_loop():
    global play_game
    play_game = input("Do You want to play again? y = yes, n = no \n")
    while play_game not in ["y", "n","Y","N"]:
        play_game = input("Do You want to play again? y = yes, n = no \n")
    if play_game == "y":
        main()
    elif play_game == "n":
        print("Thanks For Playing! We expect you back again!")
        exit()
In the part here, when the game is over, it asks you if you want to play again, if you answer yes the program is over, if you say no the program exits (as it should) and if you answer with something else, it asks you again until you answer with either yes or no, though when you answer with yes, the program stops again.
There is no error or anything, the loop just breaks off I guess.

I played around with the code but I can't find the solution. I have had this problem before because of the same reason. If answer is yes, nothing happens.

Thank you for your help
Yoriz write Oct-12-2021, 12:23 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
How to fix while loop breaking off - by raphy11574 - Oct-12-2021, 09:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 325 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 1,082 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,240 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,498 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  breaking a large program into functions, not acting as expected Zane217 9 3,035 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,377 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,596 Jan-11-2021, 07:42 AM
Last Post: kamui123
  breaking outof nexted loops Skaperen 4 3,138 Feb-07-2020, 02:04 AM
Last Post: Skaperen
  Breaking While Loop JustWonderful 4 3,078 Oct-28-2019, 01:12 AM
Last Post: JustWonderful
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,898 Sep-18-2019, 09:43 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020