布尔控制流变量未正确实现

Boolean Control flow variable is not correctly implementing

我有以下代码,但它不控制流程以允许 player2 在 playing=false 时掷骰子。谁能发现错误?基本上,它永远不会到达:RollTwoDiceP2,我不知道为什么。

注意:我尝试在 RollTwoDiceP1 中将播放(布尔变量)设置为 false,希望在返回到 playerturns() 函数时,这次它会转到 RollTwoDiceP2(Player2 turn sub ).那行不通

    def callmatrix(player1,player2, n):
    print("*************LOADING GAME******************")
    print("Welcome:", player1,"and", player2)
    for i in matrix(n):
            print(i)
    playing = True
    playerturns(player1,player2,playing)

def playerturns(player1,player2,playing):
    print(" - - - - - - - - - - ")
    print("Press Enter to contnue")
    #playing = True
    while(playing):     
        roll=input()
        if roll=="r" or "R":
            RollTwoDiceP1(player1,player2)
        else:
            RollTwoDiceP2(player1,player2)


def RollTwoDiceP1(player1,player2):
    turn=input("Player 1, it's your turn to roll the dice: Press r to roll:>>>")
    #create two variables here and assign them random numbers
    die1=random.randint(1,6)
    die2=random.randint(1,6)

    #add the two die numbers together
    roll=die1+die2

    #when you are printing an integer, you need to cast it into a string before you printit
    print("Player1: You rolled a:", die1, "and a", die2, "which give you a:", roll)

    playing = False
    playerturns(player1,player2,playing)

def RollTwoDiceP2(player1,player2):
    turn=input("Player 2, it's your turn to roll the dice: Press r to roll:>>>")
    #create two variables here and assign them random numbers
    die1=random.randint(1,6)
    die2=random.randint(1,6)

    #add the two die numbers together
    roll=die1+die2    


    print("Player2: You rolled a:", die1, "and a", die2, "which give you a:", roll)

    playing = True
    playerturns(player1,player2,7,playing)

输出:

Continually asks Player 1 to Roll. Prints the result of Player 1s roll (repeat)

这是一个逻辑错误,因此它不是指定问题的重复。

问题出在 if roll=="r" or "R": 行。首先,我们评估 roll=="r",它可能是 true 或 false,然后是 "R",它是 always true。由于它与 or 组合,因此该语句始终为真,并且 else 分支不会执行。将语句更改为 if roll == "r" or roll == "R": 或更好的 if roll.lower() == "r":