[英]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.