2017-02-02 11 views
0

私は以下のコードを持っていますが、playing = falseのときにplayer2がサイコロを動かせるようにフローを制御しません。誰もがエラーを見つけることができますか?基本的に、それは決して得られない:RollTwoDiceP2と私は理由を理解できない。ブール制御フロー変数が正しく実装されていません

注:私は、playerturns()関数に戻るときに、今回はRollTwoDiceP2(Player2 turn sub)に行くことを期待して、RollTwoDiceP1でfalse(Boolean変数)をfalseに設定しようとしました。 。それは

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) 

これは論理エラーなので、指定された問題の複製ではありません。

+0

@TidB - いいえ、私は何かをする方法を尋ねるのではなく、私のプログラムのロジックを分析し、提案を与えました –

答えて

0

問題は、行if roll=="r" or "R":です。まず、roll=="r"(trueまたはfalse)、次に"R"は常に)を評価します。 orと組み合わされているため、ステートメントは常にtrueであり、elseブランチは実行されません。ステートメントをif roll == "r" or roll == "R":またはそれ以上に変更してくださいif roll.lower() == "r":

+0

ありがとうございました。しかし、それを修正すると、同じエラーが発生します。まだプレイヤー1に何度も行きます –

+0

プレイヤー2に行くために何を入力していますか? – nbryans

+0

ああ、エラーを発見し、また、あなたの修正のおかげで。私はIF文の中にあるので、ループを終了するためにBREAKする必要がありました(私はplayerturnsのループを参照しています) –

関連する問題