【问题标题】:Error in Tic tac toe Program (Python)井字游戏程序中的错误(Python)
【发布时间】:2018-06-03 18:26:40
【问题描述】:

下面显示的代码是一个井字游戏的代码,其中有两个玩家(玩家 1 和玩家 2 是人类)。我在代码的“# Player 1 Plays now”和“# Player 2 Plays now”部分的 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()

【问题讨论】:

    标签: python-2.7 if-statement syntax-error tic-tac-toe


    【解决方案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,因此始终为 true。由于第一个语句为真,因此整个 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"
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 2017-11-06
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多