下面显示的代码是Tic tac toe游戏,其中有两个玩家(Player1和Player2是人类)。我现在在" #Player 1 Play中的if else语句中出现问题"和#34; #Player 2现在播放"代码的各个部分。确切地说,计算机总是认为每个盒子已经填充了除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()
答案 0 :(得分:1)
您的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
个关键字在逻辑上组合了每个比较。然后,第一个比较语句将只是'X'
,它不是null或0,因此总是如此。由于第一个陈述是真的,整个if语句都是正确的。
我经常看到人们开始学习编程。 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"