Repeat while loop?

Hello! So, Im trying to figure out how to repeat a while loop. I want my loop to restart completely, like if I’m making a game and I want a round two. I’ve tried looking online but it always confused me what is going on… anyone mind helping me out?

This is more a design choice, however, I would do something like

while 1:
    while condition:  # your conditional
        ...  # whatever you want?
    keep_going = input("Keep going? (y/n) ")
    if keep_going != "y":

the thing is that i dont know what to make the condition.

The best way to do this is to write a function that plays ONE game, then
another function that plays as many games as you want.

def game():
    # Play one game.

def play_games():
    # Play repeatedly.
    while True:  # Loop forever.
       game()  # Play one game.
       answer = input("Do you want to play again? Y/N ")
       if answer == 'N':

1 Like

Okay! Thank you so much! Ive got it playing multiple games… but its not asking if the user wants to continue…


Remember that indentation is significant in Python. You use indentation
to instruct the interpreter what part of your code is part of a block
and what comes after the block.

Look back at my version, that has

while True:
    ask to continue?

Notice that the game and the code asking to continue are indented at the
same level. Now look at yours. You have two while loops. It isn’t
clear to me why you need two. I would have to read your code in very
close attention to detail to work it out, but at a glance it doesn’t
seem right to me. You have:

while True:
  while win != "Xwin" or win != "Owin":
  keep = input("Continue? (y/n)\n > ")

So the inner loop, while win..., plays game after game after game
after game, I think forever, and the line where you ask the user if they
want to continue never gets called.

A few more pieces of advice:

It is conventional to use four spaces for each indent. The interpreter
doesn’t care. You could use 1 space or 1000 spaces, it’s all the same to
the interpreter. But for the human reader, the four spaces makes it more
obvious when you change level, without being too big that it wastes
space. Consider using four spaces rather than two.

Most of the time, it is better to write tests as an equality test rather
than an inequality test. Consider your test here:

win != "Xwin" or win != "Owin"

I think (at a quick glance) that win gets initialised to “h” (why?)
and then never touched again. So I think that it is always “h”. So your
test above is always true: win is always not equal to “Xwin” or “Owin”
so the inner while loop will loop forever.

You need to think more carefully about your game logic. Whether the
player wins, loses or ties shouldn’t affect whether they play again.

That is because win must be assigned to something for it to check if win still doesnt equal Xwin or Owin. If I remove them I get an error,

NameError: name ‘win’ is not defined

Btw, ive finished the code. If you still see ways of improvement then dont be afriad to tell me!


Oh! And is the indention good now? I set the indent size to four.