简体   繁体   English

我不知道为什么这个函数不起作用 [Python 3]

[英]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.我最近才开始编码,我正在尝试用 Python 3 制作一个 Tic Tac Toe 程序。这是我尝试过的最大的项目,我似乎做得很好,但我遇到了一个我无法弄清楚的问题。

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.我的棋盘编号为 1 到 9。当我使用参数 move1 和 user1 运行此函数时,它应该将棋盘上的相应位置更改为用户的字符 X 或 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.因为new_move square#被视为局部变量。

The local variables only live in new_move function.局部变量只存在于new_move函数中。 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. Naetmul 的例子是正确的,但我建议你减少你的代码。 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 ;)无论如何,我在列表中将 0 作为默认值,因为我不知道变量user是什么;)

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

相关问题 为什么此递归函数在python中不起作用 - Why this recursive function doesn't work in python 我创建了一个猜谜游戏,但我不明白为什么我的 -= 命令 function 不起作用 - I created a guessing game and I can't understand why my -= command function doesn't work 无法理解为什么功能不起作用 - Can't understand why function doesn't work 为什么这在Python中不起作用(无法分配运算符) - Why doesn't this work (can't assign the operator) in Python 为什么 SageMath 函数 find_local_maximum 在 Python 中对我不起作用? - Why doesn't the SageMath function find_local_maximum work for me in Python? 我计算最大二进制间隙的函数不起作用,但我不知道为什么 - My Function To Count The Largest Binary Gap Doesn't Work But I Can't Figure Out Why 为什么这不起作用(Python)? - Why doesn't this work (Python)? 要在python列表中查找重复项,为什么此逻辑不起作用? - To find duplicates in a python list, why doesn't this logic work? 按名称查找客户 function 也不起作用,它只搜索我添加的第一个客户,这是为什么呢? - the find customer by name function doesn't work as well, it only searching the first customer that i'm addig, why is that? 为什么我的python字典排序功能不起作用? - why my python dictionary sort function doesn't work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM