-1

I've put while True at the start of my program and the loop at the end of the game. if you press 'n', works fine. If you press 'y', doesnt work at all.

while True:
    plyagn = input("Would you like to play again? (y/n)")
    if plyagn in ("y", "n"):
        print("*** Please choose 'y' or 'n'! ***")
    if plyagn == "y":
        continue
    if plyagn == "n":
        break



                while True:
                    again = input("Would you like to play again? (y/n)")
                    if again == "y":
                        continue
                    else:
                        quit()

if i press 'n', it works perfectly fine and the game ends. However, when i press 'y', it repeats the question over and over and does not restart the game. No error messages at all.

  • 1
    You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Aug 23 '19 at 02:18
  • If the second while-loop is nested under the **if plyagn=="n":** then it will never run. The **break** command ends the first while loop before it reaches the nested loop so it wouldn't run. Otherwise, **continue** is called every time you input "y" as it is intended to. – Seraph Wedd Aug 23 '19 at 02:20
  • You are using continue when input is equal to "y". continue stops the execution of loop in the middle and loop again starts to execute from top or start. So as input equal "y" your program execution goes onto main While loop and it asks the question and nothing happens. So just put the logic inside if input == "y" condition. – learner-coder Aug 23 '19 at 02:21
  • Read about continue and pass in python looping. – learner-coder Aug 23 '19 at 02:22
  • Your first 3 ifs make no sense. Fix your indentation and take a closer look at your code, remember that `elif` is your friend. – Pedro Lobito Aug 23 '19 at 03:04

1 Answers1

0

Assuming that the posted code above is just a snippet of the whole, and is just the code for your "end game" upon which a player is to choose whether to continue or not, then this is a bit inappropriate.

For a simple, repeatable game, it should be designed like this in pseudocode:

game_over = False
while not game_over:
    #initialize/reset the game variables
    #Do the main game loop
        #Check for game over to break out of game loop
    #Ask to play again

Then, in the Ask to play again you need to manipulate the value of game_over so that it will end the game (or precisely, the while loop) if you choose "n", otherwise, resumes the loop (Goes over game initialization again).

Otherwise, you can change your code and add a function called game_reset() that resets your game's global variables to default instead of continue. It will look like:

if again=='y':
    game_reset()
    break()

Hope this helps.

Seraph Wedd
  • 864
  • 6
  • 14