我开始学习Python,我想知道为什么即使我在函数之上定义了我的变量反函数,它也不会在函数中被识别。
counterofplays = 0 # this variable should be defined below
def tiktaktoe():
while playing:
isdigit = False
inRange = False
while not isdigit or not inRange:
playable = True
print(firstchoice,"turn")
index = input("Choose the index:\n" +
"1 | 2 | 3\n" +
"4 | 5 | 6\n" +
"7 | 8 | 9\n")
if index.isdigit():
isdigit = True
index = int(index)
if index in range(1,4) and row1[index-1][0] == ' ':
inRange = True
row1[index-1] = firstchoice if index == 3 else firstchoice + " | "
counterofplays += 1 # and here it marks error
elif index in range(4,7) and row2[index-4][0] == ' ':
inRange = True
row2[index-4] = firstchoice if index == 6 else firstchoice + " | "
counterofplays += 1
elif index in range(7,10) and row3[index-7][0] == ' ':
inRange = True
row3[index-7] = firstchoice if index == 9 else firstchoice + " | "
counterofplays += 1
else:
inRange = False
playable = False
else:
isdigit = False
playable = False
print("You've to choose a number")
sleep(1.3)发布于 2022-01-24 06:48:09
您应该尝试将counterofplays直接在TikTakToe()中定义为global,并且不会造成任何问题。由于似乎没有在上述函数之外使用该变量,所以也不能将其设置为global
https://stackoverflow.com/questions/70829863
复制相似问题