Boolean控制流变量未正确实现

时间:2017-02-02 14:06:34

标签: python boolean conditional

我有以下代码,但是当play = false时,它不控制允许player2滚动骰子的流程。谁能发现错误?基本上,它永远不会达到:RollTwoDiceP2,我无法弄清楚原因。

注意:我曾尝试在RollTwoDiceP1中将播放(布尔变量)设置为false,希望在返回playerturns()函数时,此时它将转到RollTwoDiceP2(Player2转子)。这不起作用

    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)

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

1 个答案:

答案 0 :(得分:0)

问题是行if roll=="r" or "R":。首先,我们评估roll=="r",可能是真或假,然后"R"始终为真。由于它与or结合使用,因此该语句始终为true,并且else分支未执行。将语句更改为if roll == "r" or roll == "R":或更高if roll.lower() == "r":