简体   繁体   English

为什么我没有像我想的那样改变全局范围

[英]Why am I not changing the global scope the way I thought

I guess my problem has to do with global scope.我想我的问题与全局范围有关。 I'm not sure what it is that I am not understanding.我不确定我不理解的是什么。 I have computer_symbol defined in the global scope in line 6. Then the choose_symbol function on line 18 should be making the computer_symbol whatever the user doesn't choose.我在第 6 行的全局范围内定义了 computer_symbol。然后,第 18 行的 choose_symbol 函数应该使 computer_symbol 成为用户未选择的任何内容。 Then I call the function in line 30. But when I try to use the variable in line 45 and test my code, I see that computer_symbol still equals the 'nothing' value that was only ever meant as a placeholder.然后我调用第 30 行中的函数。但是当我尝试使用第 45 行中的变量并测试我的代码时,我发现 computer_symbol 仍然等于“无”值,该值仅用作占位符。

import random


board_locations = [0, 1, 2, 3, 4, 5, 6, 7, 8]

computer_symbol = 'nothing'

def draw_board():
    print('   |   |   ')
    print(f'_{board_locations[0]}_|_{board_locations[1]}_|_{board_locations[2]}_')
    print('   |   |   ')
    print(f'_{board_locations[3]}_|_{board_locations[4]}_|_{board_locations[5]}_')
    print('   |   |   ')
    print(f' {board_locations[6]} | {board_locations[7]} | {board_locations[8]} ')
    print('   |   |   ')


def choose_symbol(user_symbol):
    if user_symbol == 'X':
        computer_symbol = 'O'
    else:
        computer_symbol = 'X'

    return computer_symbol


draw_board()

user_symbol = input("Would you like to be 'X' or 'O': ")
choose_symbol(user_symbol)

game = True

while game:
    draw_board()

    chosen_location = int(input('Choose the location you want to move on the board: '))
    if chosen_location in board_locations:
        board_locations.pop(chosen_location)
        board_locations.insert(chosen_location, user_symbol)
        draw_board()
        computer_choice = random.choice(board_locations)

        board_locations.pop(computer_choice)
        board_locations.insert(computer_choice, computer_symbol)

At first, I didn't even have the computer_symbol variable because I thought that I could do that in the function choose_symbol() but the program didn't like that because computer_symbol hadn't been defined.起初,我什至没有 computer_symbol 变量,因为我认为我可以在函数 choose_symbol() 中做到这一点,但程序不喜欢那样,因为没有定义 computer_symbol。

The computer_symbol variable that you use inside the function is a local variable inside the function, so it is not the same as the global computer_symbol variable.您在函数内部使用的computer_symbol变量是函数内部的局部变量,因此它与全局computer_symbol变量不同。 To change that, you could explicitly reference the global variable inside the function by adding the statement global computer_symbol at the top of the function body.要更改它,您可以通过在函数体顶部添加语句global computer_symbol来显式引用函数内部的全局变量。

However, judging from the code you show, it is not necessary to have your function change the global variable here.但是,从您显示的代码来看,没有必要让您的函数在这里更改全局变量。 In general, it's better to avoid such side effects if you can.一般来说,如果可以的话,最好避免这种副作用。 Just delete the computer_symbol = 'nothing' assignment and assign the function's return value instead:只需删除computer_symbol = 'nothing'赋值并指定函数的返回值:

computer_symbol = choose_symbol(user_symbol)

The definition of computer_symbol is indeed global, but you cannot modify global variables inside a function. computer_symbol的定义确实是全局的,但是你不能在函数内部修改全局变量。 In order to do this, you have to add global var_name , so it looks like this:为了做到这一点,你必须添加global var_name ,所以它看起来像这样:

def choose_symbol(user_symbol):
    global computer_symbol
    if user_symbol == 'X':
        computer_symbol = 'O'
    else:
        computer_symbol = 'X'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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