Python名称错误:名称';当前玩家';没有定义
我刚刚开始使用Python,并决定做一个Tic-Tac-Toe项目。但是,我无法理解以下错误: 显示错误:Python名称错误:名称';当前玩家';没有定义,python,Python,我刚刚开始使用Python,并决定做一个Tic-Tac-Toe项目。但是,我无法理解以下错误: 显示错误: Traceback (most recent call last): File "c:/Users/a/Desktop/Visual Studio Code/Tic-Tac-Toe Project/main.py", line 57, in <module> play_game() File "c:/Users/a/Desktop/Visual Studio Code/Tic
Traceback (most recent call last):
File "c:/Users/a/Desktop/Visual Studio Code/Tic-Tac-Toe Project/main.py", line 57, in <module>
play_game()
File "c:/Users/a/Desktop/Visual Studio Code/Tic-Tac-Toe Project/main.py", line 53, in play_game
handle_turn()
File "c:/Users/a/Desktop/Visual Studio Code/Tic-Tac-Toe Project/main.py", line 29, in handle_turn
player_turn_swap()
File "c:/Users/a/Desktop/Visual Studio Code/Tic-Tac-Toe Project/main.py", line 42, in player_turn_swap
if current_player == 'X':
NameError: name 'current_player' is not defined
回溯(最近一次呼叫最后一次):
文件“c:/Users/a/Desktop/visualstudio Code/Tic-Tac-Toe-Project/main.py”,第57行,在
玩游戏
文件“c:/Users/a/Desktop/visualstudio Code/Tic-Tac-Toe-Project/main.py”,第53行,游戏中
手柄(U形)
文件“c:/Users/a/Desktop/visualstudio Code/Tic Tac Toe Project/main.py”,第29行,依次为handle\u
玩家交换
文件“c:/Users/a/Desktop/visualstudio Code/Tic-Tac-Toe-Project/main.py”,第42行,在播放器交换中
如果当前_播放器=='X':
NameError:未定义名称“当前玩家”
我的代码
#VARIABLES
board = ["_" , "_", "_",
"_" , "_", "_",
"_" , "_", "_"]
global current_player
position = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
#FUNCTIONS
def display_board():
print(board[0] + " | " + board[1] + " | " + board[2] + " 1 | 2 | 3")
print(board[3] + " | " + board[4] + " | " + board[5] + " 4 | 5 | 6")
print(board[6] + " | " + board[7] + " | " + board[8] + " 7 | 8 | 9")
def handle_turn():
display_board()
position = input("Choose an position from 1-9: ")
if position in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
position = int(position) - 1
if board[position] == 'X' or board[position] == 'O':
print("Position occupied. Go again!")
elif board[position] == '_': #if position is empty, replace it with "X" or "O"
board[position] = 'X'
current_player = 'X'
player_turn_swap()
position = input("Choose an position from 1-9: ")
board[position] = current_player
display_board()
player_turn_swap()
else: print("Invalid choice. Go again!")
def player_turn_swap(): #change turns
global current_player
if current_player == 'X':
current_player = 'O'
elif current_player == 'O':
current_player = 'X'
return current_player
def play_game():
a = 0 #Play5Times
while a < 6:
handle_turn()
a += 1
play_game() #call play_game
#变量
董事会=[“董事会”、“董事会”、“董事会”,
"_" , "_", "_",
"_" , "_", "_"]
全球当前游戏玩家
位置=[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”]
#功能
def显示板():
打印(板[0]+“|”+板[1]+“|”+板[2]+“1 | 2 | 3”)
打印(板[3]+“|”+板[4]+“|”+板[5]+“4|5|6”)
打印(板[6]+“|”+板[7]+“|”+板[8]+“7|8|9”)
def手柄转动()
显示板()
位置=输入(“从1-9中选择一个位置:”)
如果位置在[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”]:
位置=int(位置)-1
如果电路板[位置]=“X”或电路板[位置]=“O”:
打印(“位置已被占用,请再试一次!”)
elif板[position]='.':#如果position为空,则将其替换为“X”或“O”
板[位置]=“X”
当前_播放器='X'
玩家交换
位置=输入(“从1-9中选择一个位置:”)
棋盘[位置]=当前棋手
显示板()
玩家交换
其他:打印(“无效选择。请重试!”)
def player_turn_swap():#更改回合
全球当前游戏玩家
如果当前_播放器=='X':
当前_播放器='O'
elif当前_播放器=='O':
当前_播放器='X'
返回当前\u播放器
def play_game():
a=0#播放5次
而a<6:
手柄(U形)
a+=1
play_game()#调用play_game
我试过分配
全局当前_播放器='X'
在第一个声明中(在#变量中),但它不允许我这样做
请提供解决方案和一些建议(如有)。
谢谢 在文件顶部,您不需要写入
全局当前\u播放器
——而是(在文件顶部的同一位置)创建一个变量并为其指定一些初始值:
current_player = 'X' # Or some other initial value
声明一个变量<代码>全局<代码>只在一个作用域内(这意味着“不要在这里查找变量,而是使用全局范围”),而不是在文件的顶部:也可以考虑使用<代码>非本地< /代码>而不是<代码> Global < /C> >查看更多细节。
代码中存在一些问题:
在第6行,global current\u player
应该是current\u player=None
或任何其他值
然后在下面的声明中:
elif board[position] == '_': #if position is empty, replace it with "X" or "O"
# ...
position = input("Choose an position from 1-9: ")
board[position] = current_player # <----- Problem Here!!
display_board()
将当前\u player初始化为类似字符串的全局当前\u player,而不是当前\u player=''。此外,就一般知识而言,
global
是一个关于如何引用变量的声明,但它不是一个可以与其他表达式组合的表达式(所以你不能做global=
。我做了上面的更改,但是它弹出了另一个错误:文件“c:/Users/a/Desktop/visualstudio code/Tic Tac Toe Project/main.py”,第32行,在手柄翻转板[position]中=current_player TypeError:列表索引必须是整数或片,而不是strSee@mushif ali nawaz给出的以下答案,以解决第二个错误
position = int(position) - 1 # this statement is missing
board[position] = current_player