简体   繁体   中英

I can't find why this function doesn't work [Python 3]

I just started coding recently and I'm trying to make a Tic Tac Toe program in Python 3. This is the largest project I've attempted yet and I seemed to be doing fine but I ran into a problem that I cannot figure out.

I have my board numbered 1 through 9. When I run this function with arguments move1 and user1 it should change the respective spot on the board to the User's character X or O.

print("Player 1 goes first. Select your square")
move1 = input("> ")

def new_move(number_of_moves, user):
    if number_of_moves == '1':
        square1 = user
    if number_of_moves == '2':
        square2 = user
    if number_of_moves == '3':
        square3 = user
    if number_of_moves == '4':
        square4 = user
    if number_of_moves == '5':
        square5 = user
    if number_of_moves == '6':
        square6 = user
    if number_of_moves == '7':
        square7 = user
    if number_of_moves == '8':
        square8 = user
    if number_of_moves == '9':
        square9 = user

new_move(move1, user1)
tictactoe()

However when I run the function it doesn't change the value of the respective square to the User's character. It doesn't change anything at all. I've been banging my head on the wall trying to figure out why to no avail. Like I said I'm very new to this so any help is appreciated. Thank you!

Because square# in new_move is considered as local variables.

The local variables only live in new_move function. It is not referenced outside of the function.

You can change it by making them global.

def new_move(number_of_moves, user):
    global square1, square2, square3, square4, square5, square6, square7, square8, square9
    if number_of_moves == '1':
        square1 = user
    if number_of_moves == '2':
        square2 = user
    if number_of_moves == '3':
        square3 = user
    if number_of_moves == '4':
        square4 = user
    if number_of_moves == '5':
        square5 = user
    if number_of_moves == '6':
        square6 = user
    if number_of_moves == '7':
        square7 = user
    if number_of_moves == '8':
        square8 = user
    if number_of_moves == '9':
        square9 = user

Actually, I don't recommend this, but it will work.

For more information, refer to Using global variables in a function other than the one that created them

The example of Naetmul is correct but I recommend you to reduce your code. To do that, I used a list.

print("Player 1 goes first. Select your square")
move1 = input("> ")

squares = [0,0,0,0,0,0,0,0,0] # Use a list to reduce the size of your code.

def new_move(square, user):
    if square.isdigit():
        try: squares[square-1] = user
        except: print("Incorrect value.")
    else:
        print("Incorrect value.")

new_move(move1, user1)
tictactoe()

Anyway, I put 0 as default value in the list because I don't know what is the variable user ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM