Hayward
Hayward

Reputation: 3

How can I break a while loop when the requirements of a called function are met?

The program is just a very basic 'o and x' game.

In the program one function (the user input) is calling another function (to check if there is a winner).

Obviously not every turn is a winner, but needs to be checked after each go. Then if the criteria for winning is met then I want it to break the program.

I have managed to make it work using sys.exit() in the winner function however I actually want the 'user input' function to check if the 'winner' function has been fulfilled.

Here is the code, (it works). I will put a # Message in the place that I want the check to actually happen.

Winner function

import sys
def winlose(g):
    #down line 1 ### WORKING ###
    if g[0][0] == g[1][0] and g[0][0] == g[2][0] and g[0][0] != 0:
        if g[0][0] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[0][0] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    #accross line 1 ### WORKING ###
    elif g[0][0] == g[0][1] and g[0][0] == g[0][2] and g[0][0] != 0:
        if g[0][0] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[0][0] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    # diagonal top left ### WORKING ###
    elif g[0][0] == g[1][1] and g[0][0] == g[2][2] and g[0][0] != 0:
        if g[0][0] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[0][0] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    #middle down ### WORKING ###
    elif g[0][1] == g[1][1] and g[0][1] == g[2][1] and g[0][1] != 0:
        if g[0][1] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[0][1] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    #end down ### WORKING ###
    elif g[0][2] == g[1][2] and g[0][2] == g[2][2] and g[0][2] != 0:
        if g[0][2] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[0][2] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    #diagonal right ### WORKING ###
    elif g[0][2] == g[1][1] and g[0][2] == g[2][0] and g[0][2] != 0:
        if g[0][2] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[0][2] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    # middle accross ### WORKING ###
    elif g[1][0] == g[1][1] and g[1][0] == g[1][2] and g[1][0] != 0:
        if g[1][0] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[1][0] == 'o':
            print(f"Player 2 wins")
            sys.exit()
    #bottom accross
    elif g[2][0] == g[2][1] and g[2][0] == g[2][2] and g[2][0] != 0:
        if g[2][0] == 'x':
            print(f"Player 1 wins")
            sys.exit()
        elif g[2][0] == 'o':
            print(f"Player 2 wins")
            sys.exit()

User input page

from winner import winlose as w
g = [[0, 0, 0],
     [0, 0, 0],
     [0, 0, 0]]

no = 0
print("\n")
for item in g:
    print(item)
print("\n")


def player(no):
    while True:
        counter = ''
        turn = ''
        if no % 2 == 0:
            turn = 'Player 1'
            counter = 'x'
        else:
            counter = 'o'
            turn = 'Player 2'

        print(f"{turn} which square do you want to go in?")
        row = int(input('row 1, 2 or 3? ')) - 1
        column = int(input('column 1, 2 or 3? ')) - 1

        if g[row][column] == 0:
            g[row][column] = counter
            no += 1
            print("\n")
            for item in g:
                print(item)
            print("\n")

            w(g)
# I would like the check to see if someone has won the game to happen here. If it has to break the while loop 
            else:
            print('that space is taken, please go again')
            player(no)

        if no == 9:
            print("Its a draw")
            break


player(no)

Upvotes: 0

Views: 67

Answers (3)

First you should set a flag whether game ended or keep going, like:

def winlose(g):
    isGameEnded = False

then, you should change all sys.exit with

isGameEnded = True

at the end of function return it to main program,

return isGameEnded

and in the main function you can do this simply:

if(winlose(g)):
    break

full form of program;

def winlose(g):
    isGameEnded = False
    #down line 1 ### WORKING ###
    if g[0][0] == g[1][0] and g[0][0] == g[2][0] and g[0][0] != 0:
        if g[0][0] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[0][0] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    #accross line 1 ### WORKING ###
    elif g[0][0] == g[0][1] and g[0][0] == g[0][2] and g[0][0] != 0:
        if g[0][0] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[0][0] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    # diagonal top left ### WORKING ###
    elif g[0][0] == g[1][1] and g[0][0] == g[2][2] and g[0][0] != 0:
        if g[0][0] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[0][0] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    #middle down ### WORKING ###
    elif g[0][1] == g[1][1] and g[0][1] == g[2][1] and g[0][1] != 0:
        if g[0][1] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[0][1] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    #end down ### WORKING ###
    elif g[0][2] == g[1][2] and g[0][2] == g[2][2] and g[0][2] != 0:
        if g[0][2] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[0][2] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    #diagonal right ### WORKING ###
    elif g[0][2] == g[1][1] and g[0][2] == g[2][0] and g[0][2] != 0:
        if g[0][2] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[0][2] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    # middle accross ### WORKING ###
    elif g[1][0] == g[1][1] and g[1][0] == g[1][2] and g[1][0] != 0:
        if g[1][0] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[1][0] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    #bottom accross
    elif g[2][0] == g[2][1] and g[2][0] == g[2][2] and g[2][0] != 0:
        if g[2][0] == 'x':
            print(f"Player 1 wins")
            isGameEnded = True
        elif g[2][0] == 'o':
            print(f"Player 2 wins")
            isGameEnded = True
    return isGameEnded

and the user side;

def player(no):
    while True:
        counter = ''
        turn = ''
        if no % 2 == 0:
            turn = 'Player 1'
            counter = 'x'
        else:
            counter = 'o'
            turn = 'Player 2'

        print(f"{turn} which square do you want to go in?")
        row = int(input('row 1, 2 or 3? ')) - 1
        column = int(input('column 1, 2 or 3? ')) - 1

        if g[row][column] == 0:
            g[row][column] = counter
            no += 1
            print("\n")
            for item in g:
                print(item)
            print("\n")
        if(winlose(g)):
            print("game is over")
            break
        # I would like the check to see if someone has won the game to happen here. If it has to break the while loop 
    else:
        print('that space is taken, please go again')
        player(no)

    if no == 9:
        print("Its a draw")
        break

Upvotes: 0

Hazim Arafa
Hazim Arafa

Reputation: 96

Whenever you use a while loop you can just call a function and whatever it returns will be used as the condition for the loop. You can see how I flip the bool value of x every time I call the function so then it breaks. Please accept this awnser if you though this was helpful

x = False

def foo():
    x = not (x)
    return x

while foo():
    foo()
    print("weeee")

Upvotes: 0

muzzletov
muzzletov

Reputation: 688

You can do so by using the keyword "break" inside of the loop.

while True:
 break

the body is executed exactly once thanks to break

Upvotes: 1

Related Questions