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
#2
Maybe try something like

#! /usr/bin/env python3

def play():
    while True:
        play_again = input('Play Again? ')
        if play_again.lower() in ['y', 'yes']:
            print('I am playing agin')
            # Do some stuff
        elif play_again.lower() in ['n', 'no']:
            print('Thanks for playing!')
            break
        else:
            print('Please type yes or no')
            continue

play()
Output:
Play Again? yes I am playing agin Play Again? mm Please type yes or no Play Again? no Thanks for playing!
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thank you. Yeah I get that much but exactly "Do some stuff" is what bugs me. I don't know what to write here so that it returns to the beginning of the game.
Reply
#4
"Do some stuff" is where you call your function that plays the game.
def play_game():
    '''code for playing my game'''

while True:
    inp = input('Would you like to play my wonderful game? ')
    if len(inp) == 0 or inp[0].upper() != 'Y':
        break
    play_game()
This loop requires the user to input some kind of YES, Yes, yes, yup, y, Y to play otherwise it exits. If you want to play the game at least once you make a slight change.
def play_game():
    '''code for playing my game'''

while True:
    play_game()
    inp = input('Would you like to play my wonderful game? ')
    if len(inp) == 0 or inp[0].upper() != 'Y':
        break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 214 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 985 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,174 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,443 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  breaking a large program into functions, not acting as expected Zane217 9 2,931 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,322 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,435 Jan-11-2021, 07:42 AM
Last Post: kamui123
  breaking outof nexted loops Skaperen 4 3,060 Feb-07-2020, 02:04 AM
Last Post: Skaperen
  Breaking While Loop JustWonderful 4 3,010 Oct-28-2019, 01:12 AM
Last Post: JustWonderful
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,860 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