在python中运行if后,一些代码执行而其他代码不执行

时间:2018-05-30 14:52:00

标签: python

我正在创建一个tictactoe游戏,而某些代码不会在if语句中执行。

整个代码在这里:

import random
BoardValues = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
BoardValuesToWin = [0, 0, 0, 0, 0, 0, 0, 0, 0]
player1Is_X = True
player1Score = 0
player2Score = 0
switchToTurnOfO = False


def playerSymbol():
    global player1
    player1 = ""
    while not (player1 == "X" or player1 == "O"):
        player1 = input("Choose a side, it must be \"O\" or \"X\"").upper().replace(" ", "")
    if player1 == "X":
        player1Is_X = True
        playerName()
    elif player1 == "O":
        player1Is_X = False
        playerName()


def playerName():
    global player1Name
    global player2Name
    player1Name = ""
    player2Name = ""
    print(f"Player 1 defined as {player1}")
    player1Name = input("Player 1, please specify your name")
    player2Name = input("Your turn to specify your name, Player 2")
    displayBoard()


def displayBoard():
    print(f"\n{player1Name}: {player1Score}\t {player2Name}: {player2Score} ")
    print(f"       |       |       ")
    print(f"  {BoardValues[0]}    |   {BoardValues[1]}   |   {BoardValues[2]}   ")
    print(f"       |       |       ")
    print(f"-------|-------|-------")
    print(f"       |       |       ")
    print(f"  {BoardValues[3]}    |   {BoardValues[4]}   |   {BoardValues[5]}  ")
    print(f"       |       |       ")
    print(f"-------|-------|-------")
    print(f"       |       |       ")
    print(f"  {BoardValues[6]}    |   {BoardValues[7]}   |   {BoardValues[8]}  ")
    print(f"       |       |       ")
    starterPlayer()


def starterPlayer():
    randomStart = random.randint(0, 1)
    starter = "xStarts" if randomStart == 0 else "oStarts"
    if starter == "xStarts":
        switchToTurnOfO = False
        gameStartsWithX(switchToTurnOfO)
    else:
        switchToTurnOfO = True
        gameStartsWithO(switchToTurnOfO)


def gameStartsWithX(switchToTurnOfO):
    position = 0
    if player1Is_X  == True:
        if switchToTurnOfO == False:
            if (sum((BoardValuesToWin[0], BoardValuesToWin[1], BoardValuesToWin[2])) and sum(
                    (BoardValuesToWin[3], BoardValuesToWin[4], BoardValuesToWin[5])) and sum(
                    (BoardValuesToWin[6], BoardValuesToWin[7], BoardValuesToWin[8])) and sum(
                    (BoardValuesToWin[0], BoardValuesToWin[3], BoardValuesToWin[6])) and sum(
                    (BoardValuesToWin[1], BoardValuesToWin[4], BoardValuesToWin[7])) and sum(
                    (BoardValuesToWin[2], BoardValuesToWin[5], BoardValuesToWin[8]))) != 300 or -300:
                position = int(input("Take a look at the NUMPAD and choose a number for the position of your marker!"))
                BoardValues[position] = "X"
                BoardValuesToWin[position] += 100
                switchToTurnOfO = False
                displayBoardAfter_X_Played()
            else:
                pass


def gameStartsWithO(switchToTurnOfO):
    position = 0
    if player1Is_X == False:
        if switchToTurnOfO == False:
            if (sum((BoardValuesToWin[0], BoardValuesToWin[1], BoardValuesToWin[2])) and sum(
                    (BoardValuesToWin[3], BoardValuesToWin[4], BoardValuesToWin[5])) and sum(
                    (BoardValuesToWin[6], BoardValuesToWin[7], BoardValuesToWin[8])) and sum(
                    (BoardValuesToWin[0], BoardValuesToWin[3], BoardValuesToWin[6])) and sum(
                    (BoardValuesToWin[1], BoardValuesToWin[4], BoardValuesToWin[7])) and sum(
                    (BoardValuesToWin[2], BoardValuesToWin[5], BoardValuesToWin[8]))) != 300 or -300:
                position = int(input("Take a look at the NUMPAD and choose a number for the position of your marker!"))
                BoardValues[position] = "O"
                BoardValuesToWin[position] -= 100
                switchToTurnOfO = True
                displayBoardAfter_O_Played()
            else:
                pass


def displayBoardAfter_X_Played():
    print(f"\n{player1Name}: {player1Score}\t {player2Name}: {player2Score} ")
    print(f"       |       |       ")
    print(f"  {BoardValues[0]}    |   {BoardValues[1]}   |   {BoardValues[2]}   ")
    print(f"       |       |       ")
    print(f"-------|-------|-------")
    print(f"       |       |       ")
    print(f"  {BoardValues[3]}    |   {BoardValues[4]}   |   {BoardValues[5]}  ")
    print(f"       |       |       ")
    print(f"-------|-------|-------")
    print(f"       |       |       ")
    print(f"  {BoardValues[6]}    |   {BoardValues[7]}   |   {BoardValues[8]}  ")
    print(f"       |       |       ")
    gameStartsWithO(switchToTurnOfO)


def displayBoardAfter_O_Played():
    print(f"\n{player1Name}: {player1Score}\t {player2Name}: {player2Score} ")
    print(f"       |       |       ")
    print(f"  {BoardValues[0]}    |   {BoardValues[1]}   |   {BoardValues[2]}   ")
    print(f"       |       |       ")
    print(f"-------|-------|-------")
    print(f"       |       |       ")
    print(f"  {BoardValues[3]}    |   {BoardValues[4]}   |   {BoardValues[5]}  ")
    print(f"       |       |       ")
    print(f"-------|-------|-------")
    print(f"       |       |       ")
    print(f"  {BoardValues[6]}    |   {BoardValues[7]}   |   {BoardValues[8]}  ")
    print(f"       |       |       ")
    gameStartsWithX(switchToTurnOfO)

playerSymbol()

当它到达def gameStartsWithX(switchToTurnOfO):函数时,有时会执行输入,有时它不会执行输入,但是当我在那里写print("something)时,即使在输入之后,它也会在每次执行时执行,即使输入没有。

我真的不知道那是什么问题。

先谢谢。

2 个答案:

答案 0 :(得分:1)

您只需要精确:if all(s not in (300, -300) for s in (sum(...), ...., sum(...))):

答案 1 :(得分:0)

这些检查不按预期工作:

if (sum((BoardValuesToWin[0], BoardValuesToWin[1], BoardValuesToWin[2])) and 
    sum((BoardValuesToWin[3], BoardValuesToWin[4], BoardValuesToWin[5])) and 
    sum((BoardValuesToWin[6], BoardValuesToWin[7], BoardValuesToWin[8])) and 
    sum((BoardValuesToWin[0], BoardValuesToWin[3], BoardValuesToWin[6])) and 
    sum((BoardValuesToWin[1], BoardValuesToWin[4], BoardValuesToWin[7])) and 
    sum((BoardValuesToWin[2], BoardValuesToWin[5], BoardValuesToWin[8]))) != 300 or -300:

这取决于所有的总和是" truthy" (即不是0),最后的总和是!= 300,或者-300是" truthy",情况总是如此,因此整个表达式为{{ 1}}。

这是this anti-pattern的一个有趣变体,但有一个转折:您不是针对多个值测试变量,而是针对多个测试多个变量值。

您可以尝试将Trueinany合并:

all

或者你可以检查总和的绝对值:

if all(s not in (300, -300) for s in (sum(...), ...., sum(...))):

与问题无关的其他观点:

  • 您应该考虑合并if all(abs(s) != 300 for s in (sum(...), ...., sum(...))): gameStartsWithO,以及gameStartsWithXdisplayBoardAfter_X_Played函数,因为它们几乎完全相同

    < / LI>
  • 无论如何,通过在displayBoardAfter_O_Played之前定义bvtw = BoardValuesToWin然后在条件中测试if,您可以使代码更短且更具可读性

  • 好像你忘了检查对角线