-1
def initialize_board():

    board1 = ['*', '*', '*']
    board2 = ['*', '*', '*']
    board3 = ['*', '*', '*']

    print(board1)
    print(board2)
    print(board3)

    return board1
    return board2
    return board3

So I initialized my board for tic-tac-toe in this first function

And then I have this function:

import random

def input_choice():

    initialize_board()


    player_1 = input('What is your name?')
    player_2 = input('What is your name?')

    #See who goes first

    player_1_choice = input('Choose heads or tales')
    player_2_choice = input('Choose head or tales')

    if random.choice(['heads', 'tales']) == player_1_choice:
        print(f'{player_1} goes first!')
    else:
        print(f'{player_2} goes first1')

    placements = {1 : board1[0], 2 : board1[1], 3 : board1[2], 4 : board2[0], 5 : board2[1], 6 : board2[2], 7 : board3[0], 8 : board3[1], 9: board3[2]}

    print(board1)

However I get an error saying that board1 is not defined when its been defined in the first function.

NameError                                 Traceback (most recent call last)
<ipython-input-30-dd18b3b6beab> in <module>
----> 1 input_choice()

<ipython-input-29-1ebcf8044e68> in input_choice()
     19         print(f'{player_2} goes first1')
     20 
---> 21     placements = {1 : board1[0], 2 : board1[1], 3 : board1[2], 4 : board2[0], 5 : board2[1], 6 : board2[2], 7 : board3[0], 8 : board3[1], 9: board3[2]}
     22 
     23     print(board1)
NameError: name 'board1' is not defined
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Splitwire Z
  • 63
  • 1
  • 8
  • 3
    Variables in functions have scope: https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules. Furthermore, you can't simultaneously return multiple values from a function by using multiple return statements. – Matt Messersmith Nov 12 '19 at 20:42
  • Welcome to Stack Overflow! Check out the [tour]. Please [edit] your post to have a descriptive title. Also read [ask] and [mre]. – wjandrea Nov 12 '19 at 20:43
  • 1
    There are a couple of flaws in your code. The actual error comes from the fact that `board1` is only defined within your function `initialize_board()` and the variable is local. Additionally you cannot have multiple return statements within one function. – Jan Nov 12 '19 at 20:43
  • 1
    And, with the above, you never assign the output of `initialize_board()` to anything so the variable returned doesn't exist – G. Anderson Nov 12 '19 at 20:43
  • Use return board1, board2, board3 in initialize_board. And have board1, board2, board3 = initialize_board(). – DarrylG Nov 12 '19 at 20:44

3 Answers3

1

When you define two different functions, their local variables exist in different scopes. So the local variables inside initialize_board() are not visible to the function input_choice() until you return any of those values.

Another thing to note is that when you reach the first return statement in initialize_board(), i.e. when you reach return board_1, the function literally returns control to the point from where it was called. So you never actually reach the other two return statements.

How can you fix this?

Return all the three variables in one statement like this:

def initialize_board(): 
    # your code
    return board_1, board_2, board_3

And now in the caller function, assign these returned values to some other variables that are local to input_choice() function.

def input_choice():

    # these variables do not have to have the same name as the variables being returned.
    # So b1, b2, b3 = initialize_board() is also a valid statement
    # these variables are local to this function
    board_1, board_2, board_3 = initialize_board()
    ...
    # rest of your code
Rachayita Giri
  • 487
  • 5
  • 17
0

Your initialize_board function isn't returning the board correctly, and your input_choice function isn't looking at the return value. There is no value for board1 within the body of input_choice, which is why you get an error when you try to access it.

Change your initialize_board to return all three rows:

return board1, board2, board3

and then in input_choice make sure you actually retrieve those values when you call initialize_board:

board1, board2, board3 = initialize_board()
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

board1 is a variable that is scoped to your initialize_board function. You'll need to set the return of initialize_board in input_choice to a variable and use that. You also won't be able to return on three separate lines like that in initialize_board. Maybe put those three lists into a dictionary and return that in your initialize_board function.

EJ Morgan
  • 722
  • 1
  • 6
  • 8