无法从函数中变量

时间:2017-05-28 19:12:29

标签: python function

这是我为简单的tic-tac-toe赋值创建的函数。它似乎没有为变量Turn返回正确的数字,尽管它在IPython的输出中显示了正确的值。任何人都可以帮助我吗?

#INITIALIZE VARIABLES
CELLS = [1,2,3,4,5,6,7,8,9]

Taken_position = []
X_positions = []
O_positions = []    
Computer_player = ""
Turn = 0 # turn 1 is X

def play_game(Computer_pla   yer, Taken_position, X_positions, O_positions, 
    CELLS, Turn):
    #print("enter")
    if Computer_player == 'X':
    #Turn % 2 != 0: # This is player x
        Turn += 1
        play_position = input("Player X : What square do you want to play?")
        test_num = (int(play_position))
        while test_num in Taken_position:
            play_position = input("Player X : That square is taken!! Try again?")
        add_num = (int(play_position)-1)
        Taken_position.append(play_position) # keep track of all position played
        X_positions.append(play_position) # Keep track of X player, test winner
        CELLS[add_num] = 'X'
    else: # this is O player
        Turn += 1
        play_position = input("Player O : What square do you want to play?")
        test_num2 = (int(play_position))
        while test_num2 in Taken_position:
            play_position = input("Player O : That square is taken!! Try again?")
        add_num = (int(play_position)-1)
        Taken_position.append(play_position) # keep track of all position played
        O_positions.append(play_position) # Keep track of o player, test winner
        CELLS[add_num] = 'O'
    return Taken_position, X_positions, O_positions, CELLS, Turn 

#RUN FUNCTION
play_game(Computer_player, Taken_position, X_positions, O_positions, CELLS, Turn)

这是我从IPython获得的输出。它显示了正确的输出,但是当我离开函数时,Turn变量似乎没有增加。

    Player O : What square do you want to play? 2
    Out[318]: (['2'], [], ['2'], [1, 'O', 3, 4, 5, 6, 7, 8, 9], 1)

1 个答案:

答案 0 :(得分:1)

函数可以修改全局范围内的可变对象,但是Turn之类的整数不能修改,因为它们是不可变的。相反,您可能希望通过获取函数的结果并将其分配给初始化的变量,使其明确显示您的函数正在修改的内容。当你打电话:

#RUN FUNCTION
play_game(Computer_player, Taken_position, X_positions, O_positions, CELLS, Turn)

您不会将返回的值分配给任何内容,因此您看到的任何更改都来自正在更新的全局变量。如果它们不可变,那么您将看不到任何变化。

这段代码(我认为)通过您的函数返回的任何内容更新全局变量,从而使您现有的代码库工作。但是,如果您选择已选择的数字,它将进入无限循环。这是因为在test_num循环期间永远不会重新计算while test_num in Taken_position:。这是你需要研究的东西。你也没有对Computer_player做任何事情,所以轮流没有交替,但我已经解决了这个问题。

#INITIALIZE VARIABLES
CELLS = [1,2,3,4,5,6,7,8,9]

Taken_position = []
X_positions = []
O_positions = []    
Computer_player = ""
Turn = 0 # turn 1 is X

def play_game(Computer_player, Taken_position, X_positions, O_positions, 
    CELLS, Turn):
    #print("enter")
    if Computer_player == 'X':
    #Turn % 2 != 0: # This is player x
        Turn += 1
        play_position = input("Player X : What square do you want to play?")
        test_num = (int(play_position))
        while test_num in Taken_position:
            play_position = input("Player X : That square is taken!! Try again?")
        add_num = (int(play_position)-1)
        Taken_position.append(play_position) # keep track of all position played
        X_positions.append(play_position) # Keep track of X player, test winner
        CELLS[add_num] = 'X'
        Computer_player = 'O'
    else: # this is O player
        Turn += 1
        play_position = input("Player O : What square do you want to play?")
        test_num2 = (int(play_position))
        while test_num2 in Taken_position:
            play_position = input("Player O : That square is taken!! Try again?")
        add_num = (int(play_position)-1)
        Taken_position.append(play_position) # keep track of all position played
        O_positions.append(play_position) # Keep track of o player, test winner
        CELLS[add_num] = 'O'
        Computer_player = 'X'
    return Computer_player, Taken_position, X_positions, O_positions, CELLS, Turn 

#RUN FUNCTION
for x in range(5):
    (Computer_player, Taken_position, X_positions, 
    O_positions, CELLS, Turn) = play_game(Computer_player, Taken_position, 
                                        X_positions, O_positions, CELLS, Turn)