Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was making tic tac toe basing on code I got 'The Big Book of Small Python Projects - 81 Easy Practice Programs'. Everything was fine until
this line of code
while not valid_space(game_board,move):
            print(f'What is {current_player}\'s move?(1-9)')
            move = input('>')


My question is when a condition inside a while loop is false, is that condition skipped

What I have tried:

ALL_SPACES = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

def board():
    board={}
    for space in ALL_SPACES:
        board[space] = ''
    return board

def displayBoard(board):    
    return f"{board['1']} | {board['2']} | {board['3']}1,2,3\n--+---+--\n{board['4']} | {board['5']} | {board['6']} 4,5,6\n--+---+--\n{board['7']} | {board['8']} | {board['9']} 7,8,9"

def valid_space(board,space):
    return space in ALL_SPACES and board[space] == ''

def is_full(board):
    for space in ALL_SPACES:
        if board[space] == '':
            return False
    return True

def updateBoard(board,space,mark):
    board[space] = mark

def isWinner(board,player):
    '''check for winner in all rows,columns and diagonals'''
    return (
    board['1'] == board['2'] == board['3'] == player or 
    board['4'] == board['5'] == board['6'] == player or
    board['7'] == board['8'] == board['9'] == player or
    board['1'] == board['4'] == board['7'] == player or
    board['2'] == board['5'] == board['8'] == player or
    board['3'] == board['6'] == board['9'] == player or
    board['1'] == board['5'] == board['9'] == player or
    board['3'] == board['5'] == board['7'] == player
    )
        



def game():
    print('Welcome to tic tac toe')
    game_board = board()
    current_player,next_player = 'X','Y'
    while True:
        print(displayBoard(game_board))
        move=None
        while not valid_space(game_board,move):
            print(f'What is {current_player}\'s move?(1-9)')
            move = input('>')
        updateBoard(game_board,move,current_player)
        if isWinner(game_board,current_player):
            print(displayBoard(game_board))
            print(f'{current_player} won the game')
            break
        elif is_full(game_board):
            print('It was a tie')
            break
        current_player,next_player=next_player,current_player    
         
game()
Posted
Updated 16-Jul-21 7:43am

The "rules" of a while loop are simple: the condition is tested before each possible time round the loop, including the first.
If the condition is true, the loop body is executed once, and the condition is tested again.
If the condition is false, the loop body is not executed and the statements immediately after the the loop are executed instead.
So if the condition is "x less than 2" and x starts at 0 and is incremented in the loop body, the condition will be true when the loop starts so x is incremented to one.
Since that is less than 2, the condition will again be true, so the loop body is excuted again, and x is incremented to two..
This time, the condition is false so the loop body is skipped and the following code is executed instead.
 
Share this answer
 
while not valid_space(game_board,move):
            print(f'What is {current_player}\'s move?(1-9)')
            move = input('>')


Ok, take this with a grain of salt. I'm not a python programmer, but the 'not' in the code above switches a false to a true and a true to a false.

So if valid_space(game_board, move) returns true the 'not' changes it to false and then it is processed by the while. The reverse is also true. if that function returns false, it is changed to true and the while processes it.

If you look above this function you have another While. This one is just While True. Meaning it will loop forever or until a break.

I'm reading this to say: while there is not a valid space ask about the current move.
What does the book say about that section of code?

Again take it with a grain of salt as I don't write in python. But logic is logic.

The answer to your question is that you are right. A while loop processes the contents as long as it is true. Once that condition changes to false. The while loop will not execute it's statements.

I'm sure this while loop forces a player to only enter a 1-9, any other key will repeat the loop asking the question again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900