sebastian sebastian - 26 days ago 6
Python Question

calling a function while in it

def move(list, wins_1, wins_2):
global turn
if turn % 2 == 0:
sign = "| x "
else:
sign = "| o "
y_1 = int(input("Type the value of y: "))
x_1 = int(input("Type the value of x: "))

if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ":
print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1])
move(list, wins_1, wins_2)
else:
list[y_1 - 1][x_1 - 1] = sign

print_board(list) #

wins_1, wins_2 = check_winner(sign, wins_1, wins_2)

turn += 1
return wins_1, wins_2


If the user input the [x][y] of a list and its already taken by a x or o (its a tic tac toe game) it should print("this place is already filled") and then make the user type another set of x and y to put his x/o.

That made me think, maybe I could just simply call the function and make it repeat itself. It worked fine, no error.
But for some reason it printed my board two (or for how many times i have pressed the "x" and "y" for an already filled place) many times.

Can someone explain what happens when you call a function when you are in the function. And what exactly happened in my code?

NOTE: my code is very long this is just a little bit of it. If there is a need for more code, let me know.

Here is an example of the output: note that it prints the board 2 times after putted the x/o in an already filled place.

Type the name of player 1: 1
type the name of player 2: 1
____________________________________________________________
player_1: 1 X wins: 0
player_2: 1 O wins: 0
____________________________________________________________
Type the value of y: 1
Type the value of x: 1
-------------
| x | | |
-------------
| | | |
-------------
| | | |
-------------
____________________________________________________________
player_1: 1 X wins: 0
player_2: 1 O wins: 0
____________________________________________________________
Type the value of y: 1
Type the value of x: 1
The place is already filled by | x |
Type the value of y: 1
Type the value of x: 2
-------------
| x | o | |
-------------
| | | |
-------------
| | | |
-------------
-------------
| x | o | |
-------------
| | | |
-------------
| | | |
-------------
____________________________________________________________
player_1: 1 X wins: 0
player_2: 1 O wins: 0
____________________________________________________________
Type the value of y:

Answer

Here is the answer:

def move(list, wins_1, wins_2):
    printed = False
    global turn
    if turn % 2 == 0:
        sign = "| x "
    else:
        sign = "| o "

    y_1 = int(input("Type the value of y: "))
    x_1 = int(input("Type the value of x: "))

    if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ":
        print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1])
        move(list, wins_1, wins_2)
        printed = True
    else:
        list[y_1 - 1][x_1 - 1] = sign

    if printed == False:
        print_board(list)  

    wins_1, wins_2 = check_winner(sign, wins_1, wins_2)

    turn += 1
    return wins_1, wins_2

I have added a variable called printed so it only prints once

This then allows your program only to print if it hasn't printed already

Hope this has helped :)

Comments