gpt4 book ai didi

python-2.7 - 井字游戏程序(Python)中的错误

转载 作者:行者123 更新时间:2023-12-03 08:08:00 25 4
gpt4 key购买 nike

下面显示的代码是井字游戏的代码,其中有两个玩家(Player1和Player2是人类)。我在代码的“#Player 1现在播放”和“#Player 2现在播放”部分的if else语句中遇到问题。确切地说,计算机始终认为每个框都已经填充了1,2,3,4,5,6,7,8,9以外的其他值,并且它一直显示消息“该单元格已被标记。请请尝试在嵌套的else语句中定义的另一个单元格。
有人可以启发我如何解决这个问题吗?

board = [0,1,2,3,4,5,6,7,8,9]
print type(board[1])
def board_invoke():
print "| ",board[1], " | ", board[2], " | ", board[3], " | ", "\n", "-------------------", "\n", "| ", board[4], " | ",board[5], " | ", board[6], " | ", "\n", "-------------------", "\n", "| ", board[7], " | ",board[8], " | ", board[9], " | "

def game_start():
Player1= raw_input("Select Player 1 between X or O : ")
if Player1 not in ('X','x','O','o'):
print "That is not an expected player"
game_start()
else:
print "\nSince you have selected Player 1 as %s" %Player1
print "\nThe Player 2 is assigned :",
if Player1 in ('X','x'):
Player2 = ("O")
else:
Player2 = ("X")
print Player2
print "\nThe game Starts now....\n"
board_invoke()
while (1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9) in board:

# Winning Condition
if board[1]==board[2]==board[3]==Player1:
print Player1," wins"
break
elif board[4]==board[5]==board[6]==Player1:
print Player1, " wins"
break
elif board[7]==board[8]==board[9]==Player1:
print Player1, " wins"
break
elif board[1]==board[2]==board[3]==Player2:
print Player2, " wins"
break
elif board[4]==board[5]==board[6]==Player2:
print Player2, " wins"
break
elif board[7]==board[8]==board[9]==Player2:
print Player2, " wins"
break
elif board[1]==board[5]==board[9]==Player1:
print Player1, " wins"
break
elif board[3]==board[5]==board[7]==Player1:
print Player1, " wins"
break
elif board[1]==board[5]==board[9]==Player2:
print Player2, " wins"
break
elif board[3]==board[5]==board[7]==Player2:
print Player2, " wins"
break
elif board[1]==board[4]==board[7]==Player1:
print Player1, " wins"
break
elif board[2]==board[5]==board[8]==Player1:
print Player1, " wins"
break
elif board[3]==board[6]==board[9]==Player1:
print Player1, " wins"
break
elif board[1]==board[4]==board[7]==Player2:
print Player2, " wins"
break
elif board[2]==board[5]==board[8]==Player2:
print Player2, " wins"
break
elif board[3]==board[6]==board[9]==Player2:
print Player2, " wins"
break

# Player 1 Plays now

Cell_no = raw_input("Player 1 : Please select a number you want to mark....")
Cell_no = int(Cell_no)

if Cell_no not in board:
print "Please enter a cell number within the scope of available cells"
else:
if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
print "That cell is already marked. Please try another cell"
continue
else:
board[Cell_no] = Player1
board_invoke()

# Player 2 Plays now

Cell_no = raw_input("Player 2 : Please select a number you want to mark....")
Cell_no = int(Cell_no)

if Cell_no not in board:
print "Please enter a cell number within the scope of available cells"
else:
if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
print "That cell is already marked. Please try another cell"
continue
else:
board[Cell_no] = Player2
board_invoke()

print "Do you want to play again ?"
user_decision = raw_input("Please type Yes or No : ")
if user_decision == ('YES' or 'Yes' or 'yes'):
game_start()
else:
print "Ok. I take it that we will wrap up !"
print "See you again !"



game_start()

最佳答案

您的if陈述并不完全正确。
您正在评估:

else:
if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
print "That cell is already marked. Please try another cell"


这始终是正确的。 or关键字在逻辑上合并每个比较。然后,第一个compare语句将只是 'X',它不为null或0,因此始终为true。由于第一个陈述为真,因此整个if陈述为真。

我经常在开始学习编程的人们身上看到这一点。如果陈述不能完全像人类句子那样书写。您必须将每个字符与变量进行比较。
这是解决您的问题的简单方法(不是特定于python的,而且很丑陋):
else:
if 'X' == board[Cell_no] or 'x' == board[Cell_no] or 'O' == board[Cell_no] or 'o' == board[Cell_no]:
print "That cell is already marked. Please try another cell"

但是,更好的方法是将检查结果转为列表并将其与列表进行比较:
else:
if board[Cell_no] in ['X','x', 'O', 'o']:
print "That cell is already marked. Please try another cell"

关于python-2.7 - 井字游戏程序(Python)中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47918822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com