简体   繁体   中英

Control flow is coming out of while loop though no such condition is stated in Python

Have coding experience but new to python , was trying to make a tic-tac-toe game using python. I don't know till now how to debug in jupyter notebook. So looking for some help to understand what I am doing wrong here.

Below is my code:

    while game_on:
         player1 = input('Enter the choice for player1(X/O):')
         if player1.upper() == 'X':
             print("Player 1 will go first and choice made is 'X'")
             player1_turn = True
             player2 = 'O'
         else:
             print("Player 1 will go first and choice made is 'O'")
             player1_turn = True
             player2 = 'X'
         while player1_turn:
             display_board(board)
             position = int(input("player1: Enter the position where you want to place an 'X' or 'O'(1-9):"))
             board[position] = player1.upper()
             list1.append(position)
             player1_turn = False
             player2_turn = True 
             player_win = win(board)
             if player_win:
                display_board(board)
                player1_turn = False
                player2_turn = False
                game_st = input('Would you like to play another game(y/n):')
                if game_st.upper() == 'Y':
                    game_on = True
                else:
                    game_on = False
             break  
         else:
             display_board(board)
             position = int(input("Player2: Enter the position where you want to place an 'X' or 'O' (1-9):"))
             board[position] = player2.upper()
             list1.append(position)
             player1_turn = True
             player2_turn = False

When I am executing my code and the control comes to the 'else' part of the inner while loop after the second statement (marked in bold), the control will go to the first statement of the outer while loop (marked in bold) automatically, though it should go and back to the inner while loop to get the turn of player 1 again.

Please, guide and help to understand. Many thanks MK

The problem with your code is (I think!) your use of else as part of your while loop. The else statement in a while loop in python execute only if the condition of the while loop is not satisfied on the first pass. When you enter, then break it will not execute. Try the following to see the behaviour:

while True:
    print("Runs!!!")
    break
else:
    print("Doesn't???")

while False:
    print("But this doesn't!!!")
    break
else:
    print("And this does???")

Notice that in the first case the print in the while block runs, while in the second only the else block runs.

In your case, rather than using the else statement, you probably want to do something different. Maybe a second while loop for player 2 would work? I don't want to direct how you should do it too much, but if it's helpful I can edit to give a working example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM