0

So, I'm learning Python and decided to make a Tic Tac Toe game so that you can play it in console several times. And I thought the single best way to do so is to make everything in functions so you can call them over and over again after you finished playing... I didn't really do any progress 'cos after I try call in_def_player_1_move variable it doesn't exist despite the fact I returned in function. What am I doing wrong?

def board():
    game_board = [[0, 0, 0],
                  [0, 0, 0],
                  [0, 0, 0]]
    print("   a  b  c")
    for count, row in enumerate(game_board):
        print(count, row)

# player 1 turn function
def def_player_1_turn():
    in_def_player_1_move = input()
    return in_def_player_1_move

# start of the game
def def_do_u_want_2_play():
    my_while = True

    while my_while:
        print("Do you want to play? (y/n)")
        user_choice = input("")
        if user_choice == "y":
            my_while = False
            print("Let's start!")
            print("Crosses go first. Pick your square")
            def_player_1_turn()
            player_moves_while = True
            while player_moves_while:
                if in_def_player_1_move # why doesn't this variable appear??? I returned it
                pass

        elif user_choice == "n":
            print("Bye!")
            break
        else:
            print('ERROR: please enter your choice again')


def_do_u_want_2_play()
VTi
  • 1,309
  • 6
  • 14
synalice
  • 188
  • 8
  • I do understand that how I am doing this is not the best way to make this game but I am just concerned in why doesn't what I am doing work – synalice Jun 15 '20 at 13:25

3 Answers3

1

When you return from a function, you are only returning the value that the variable contains. The name of that variable is still local to that function.

You need to assign the result to a new variable and check that, e.g.

move = def_player_1_turn()
# ...
if move:
    # ...
0x5453
  • 12,753
  • 1
  • 32
  • 61
0

Your variable is scoped to the function, so the name in_def_player_1_move is actually two different variables -- one in def_player_1_turn and the other in def_do_u_want_2_play. They have independent values. You need to get the return value from the one and use it in the other:

in_def_player_1_move = def_player_1_turn()

Short description of the scoping rules?

kojiro
  • 74,557
  • 19
  • 143
  • 201
0

You have returned your variable from your function def_player_1_turn() but you have not handled the returned value. You need to assign it to a variable name so that you can use it.

print("Crosses go first. Pick your square")
returned_variable = def_player_1_turn()
player_moves_while = True
while player_moves_while:
    if returned_variable:
        pass

or if you want to use it directly you could call your function def_player_1_turn() after your if statement:

print("Crosses go first. Pick your square")
player_moves_while = True
while player_moves_while:
    if def_player_1_turn():
        pass

Good luck

Hoppo
  • 1,130
  • 1
  • 13
  • 32