k.lo
k.lo

Reputation: 423

how to continue remaining part of execution even after the nested if condition failed

I am just trying to verify a tic tac toe board whose values I have in a list of list.

board = ([1, 2, 0], [2, 1, 0], [2, 1, 2])


def print_winner(variable):
    if variable == 1:
        return print("Winner of this board is Player1")
    else:
        return print("Winner of this board is Player2")


def interpret(game_board):
    if game_board[0][0] != 0:
        if game_board[0][1] and game_board[0][2] == game_board[0][0]:
            print(print_winner(game_board[0][0]))
        elif game_board[1][0] and game_board[2][0] == game_board[0][0]:
            print(print_winner(game_board[0][0]))
        elif game_board[1][1] and game_board[2][2] == game_board[0][0]:
            print(print_winner(game_board[0][0]))
    elif game_board[0][1] != 0:
        if game_board[1][1] and game_board[2][1] == game_board[0][1]:
            print(print_winner(game_board[0][0]))
    elif game_board[0][2] != 0:
        if game_board[1][2] and game_board[2][2] == game_board[0][2]:
            print(print_winner(game_board[0][0]))
        elif game_board[1][2] and game_board[2][0]:
            print(print_winner(game_board[0][0]))
    elif game_board[1][0] != 0:
        if game_board[1][1] and game_board[1][2] == game_board[1][0]:
            print(print_winner(game_board[0][0]))
    elif game_board[2][0] != 0:
        if game_board[2][1] and game_board[2][2] == game_board[2][0]:
            print(print_winner(game_board[0][0]))
    else:
        print("No Winner")


interpret(board)

what is the issue with above code is I don't know how to instruct my program to continue with the remaining piece of code once all my first nested if statements failed. Because of which my code is not giving any output. Expected output is "No winner".

Upvotes: 1

Views: 44

Answers (1)

MSeifert
MSeifert

Reputation: 152745

The first problem is that you are using and incorrectly. You need to specify the conditions on either operand of and. For example:

if game_board[0][1] == game_board[0][0] and game_board[0][2] == game_board[0][0]  

OR

if game_board[0][1] == game_board[0][2] == game_board[0][0] 

instead of:

if game_board[0][1] and game_board[0][2] == game_board[0][0]

However you could also use a set and check if it only contains one number:

if len({game_board[0][0], game_board[0][1], game_board[0][2]}) == 1:

The actual question about

how to instruct my program to continue with the remaining piece of code once all my first nested if statements failed

is actually quite easy. Use normal ifs and return if one condition matched.

Together with some other improvements (you always printed the game_board[0][0]) it would look like this:

def interpret(game_board):
    if game_board[0][0] != 0:
        if len({game_board[0][0], game_board[0][1], game_board[0][2]}) == 1:
            return print(print_winner(game_board[0][0]))
        elif len({game_board[0][0], game_board[1][0], game_board[2][0]}) == 1:
            return print(print_winner(game_board[0][0]))
        elif len({game_board[0][0], game_board[1][1], game_board[2][2]}) == 1:
            return print(print_winner(game_board[0][0]))
    if game_board[0][1] != 0:
        if len({game_board[0][1], game_board[1][1], game_board[2][1]}) == 1:
            return print(print_winner(game_board[0][1]))
    if game_board[0][2] != 0:
        if len({game_board[0][2], game_board[1][2], game_board[2][2]}) == 1:
            return print(print_winner(game_board[0][2]))
        elif len({game_board[0][2], game_board[1][1], game_board[2][0]}) == 1:
            return print(print_winner(game_board[0][2]))
    if game_board[1][0] != 0:
        if len({game_board[1][0], game_board[1][1], game_board[1][2]}) == 1:
            return print(print_winner(game_board[1][0]))
    if game_board[2][0] != 0:
        if len({game_board[2][0], game_board[2][1], game_board[2][2]}) == 1:
            return print(print_winner(game_board[2][0]))
    return print("No Winner")

This works because print returns None and prints to the stdout.

Upvotes: 1

Related Questions