2016-05-25 54 views
0

我正在將Python豬遊戲作爲一項業餘愛好。我的具體問題是關於第二個if聲明。爲什麼它在前面if陳述中設置爲player1roll = 'End Turn'Python遊戲如果執行語句

import random, easygui 
player1score = 0 
player2score = 0 
player1turnTotal = 0 
player2turnTotal = 0 
player1name = easygui.enterbox ('Player 1 please enter your name: ') 
player2name = easygui.enterbox ('Player 2 please enter your name: ') 
dice1roll = 0 
dice2roll = 0 
player1roll = 'Roll Again' 
player2roll = 'Roll Again' 
player1checkScore = 0 
player2checkScore = 0 
while player1roll == 'Roll Again' or player2roll == 'Roll Again': 
     while player1roll != 'End Turn' and player2score < 100 and player2roll != 'Quit' and player1roll != 'Quit': 
       dice1roll = random.randint (1, 7) 
       dice2roll = random.randint (1, 7) 
       if dice1roll == 1 and dice2roll == 1: 
         print 'Player 1 if 1.' 
         player1score = 0 
         player1turnScore = 0 
         dice1 = str (dice1roll) 
         dice2 = str (dice2roll) 
         # print 'You rolled a 1 and a 1' 
         # print player1name, 'your total score is 0' 
         easygui.msgbox (player1name + ' you rolled a ' + dice1 + ' and a ' + dice2) 
         easygui.msgbox (player1name + ' your total score is: 0') 
         player1roll = 'End Turn' 
         player2roll = 'Roll Again' 
         player1turnTotal = 0 
       if dice1roll == 1 or dice2roll == 1 and player1roll != 'End Turn': 
         print 'Player 1 if 2.' 
         player1turnScore = 0 
         dice1 = str (dice1roll) 
         dice2 = str (dice2roll) 
         player1score = player1score + player1turnScore 
         player1total = str (player1score) 
         easygui.msgbox (player1name + ' you rolled a ' + dice1 + ' and a ' + dice2) 
         easygui.msgbox (player1name + ' your total score at turns end is: ' + player1total) 
         player1roll = 'End Turn' 
         player2roll = 'Roll Again' 
         player1turnTotal = 0 
       if dice1roll != 1 and dice2roll !=1 and player1roll != 'End Turn': 
         print 'Player 1 if 3.' 
         dice1 = str (dice1roll) 
         dice2 = str (dice2roll) 
         easygui.msgbox (player1name + ' you rolled a ' + dice1 + ' and a ' + dice2) 
         player1turnScore = dice1roll + dice2roll 
         player1turnTotal = player1turnTotal + player1turnScore 
         player1total = str (player1turnTotal) 
         player1overall = str (player1score) 
         easygui.msgbox (player1name + ' your total turn score is: ' + player1total + ' and your total score is: ' + player1overall) 
         player1checkScore = player1score + player1turnTotal 
         if player1checkScore >= 100: 
          print 'Player 1 if 3.1.' 
          easygui.msgbox ("Congratulations, you've won " + player1name + "!!!") 
          player2roll = 'End Turn' 
          player1roll = 'End Turn' 
         if player1checkScore < 100: 
          print 'Player 1 if 3.2.' 
          player1roll = easygui.buttonbox (player1name, ' roll or end turn?', choices = ['Roll Again', 'End Turn', 'Quit']) 
         if player1roll == 'End Turn' and player1checkScore < 100: 
           print 'Player 1 if 3.3.' 
           player1score = player1score + player1turnTotal 
           player1endTotal = str (player1score) 
           easygui.msgbox (player1name + ' Your total score at turns end is: ' + player1endTotal) 
           player1roll = 'End Turn' 
           player2roll = 'Roll Again' 
           player1turnTotal = 0 

     while player2roll != 'End Turn' and player1score < 100 and player2roll != 'Quit' and player1roll != 'Quit': 
       dice1roll = random.randint (1, 7) 
       dice2roll = random.randint (1, 7) 
       if dice1roll == 1 and dice2roll == 1: 
         print 'Player 2 if 1.' 
         player2score = 0 
         player2turnScore = 0 
         dice1 = str (dice1roll) 
         dice2 = str (dice2roll) 
         # print 'You rolled a 1 and a 1' 
         # print player2name, 'your total score is 0' 
         easygui.msgbox (player2name + ' you rolled a ' + dice1 + ' and a ' + dice2) 
         easygui.msgbox (player2name + ' your total score is: 0') 
         player2roll = 'End Turn' 
         player1roll = 'Roll Again' 
         player2turnTotal = 0 
       if dice1roll == 1 or dice2roll == 1 and player2roll != 'End Turn': 
         print 'Player 2 if 2.' 
         player2turnScore = 0 
         dice1 = str (dice1roll) 
         dice2 = str (dice2roll) 
         player2score = player2score + player2turnScore 
         player2total = str (player2score) 
         easygui.msgbox (player2name + ' you rolled a ' + dice1 + ' and a ' + dice2) 
         easygui.msgbox (player2name + ' your total score at turns end is: ' + player2total) 
         player2roll = 'End Turn' 
         player1roll = 'Roll Again' 
         player2turnTotal = 0 
       if dice1roll != 1 and dice2roll !=1 and player2roll != 'End Turn': 
         print 'Player 2 if 3.' 
         dice1 = str (dice1roll) 
         dice2 = str (dice2roll) 
         easygui.msgbox (player2name + ' you rolled a ' + dice1 + ' and a ' + dice2) 
         player2turnScore = dice1roll + dice2roll 
         player2turnTotal = player2turnTotal + player2turnScore 
         player2total = str (player2turnTotal) 
         player2overall = str (player2score) 
         easygui.msgbox (player2name + ' your total turn score is: ' + player2total + ' and your total score is: ' + player2overall) 
         player2checkScore = player2turnTotal + player2score 
         if player2checkScore >= 100: 
          print 'Player 2 if 3.1.' 
          easygui.msgbox ("Congratulations, you've won " + player2name + "!!!") 
          player1roll = 'End Turn' 
          player2roll = 'End Turn' 
         if player2checkScore < 100: 
          print 'Player 2 if 3.2.' 
          player2roll = easygui.buttonbox (player2name, ' roll or end turn?', choices = ['Roll Again', 'End Turn', 'Quit']) 
         if player2roll == 'End Turn' and player2checkScore < 100: 
          print 'Player 2 if 3.3.' 
          player2score = player2score + player2turnTotal 
          player2endTotal = str (player2score) 
          easygui.msgbox (player2name + ' Your total score at turns end is: ' + player2endTotal) 
          player2roll = 'End Turn' 
          player1roll = 'Roll Again' 
          player2turnTotal = 0 
+0

因爲'diceroll'之一是'1'? – linusg

回答

0

其中:

if dice1roll == 1 or dice2roll == 1 and player1roll != 'End Turn': 

如果dice1roll == 1爲真,則整個語句由於操作順序而通過。在這些分組看看:

>>> (1 or 0) and 0 
0 
>>> 1 or (0 and 0) 
1 
>>> 1 or 1 and 0 
1 
>>> (1 or 1) and 0 
0 
>>> 1 or (1 and 0) 
1 
>>> 0 or 1 and 0 
0 
>>> (0 or 1) and 0 
0 
>>> 0 or (1 and 0) 
0 

請改用以下:

if (dice1roll == 1 or dice2roll == 1) and player1roll != 'End Turn': 
+0

非常感謝!那樣做了。我不知道if語句中的運算符優先級。謝謝大家! – Luis

0

我想 「第二if語句」 你提到的是這一個:

if dice1roll == 1 or dice2roll == 1 and player1roll != 'End Turn': 

之間的區別這一個和前一個是第一個說「如果A B,做這個」,而第二個說「如果A B 和C,這樣做「。

如果A和B都爲真(如果其中一個爲假,那麼該語句爲假),「和」運算符將聲明爲真而不是。如果A B爲真(兩個都可以爲爲真,但如果只有一個爲真,則該語句爲真),則「或」運算符將使該語句成爲真實。所以,雖然你需要A和B對於「和」語句是正確的,但只有A或B(或兩者)才能用「或」邏輯運算符聲明爲真。

看一看文檔:https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not(注:這是Python 2.7版,但我想在Python 3相同的作品)

編輯:我想我是緩慢的,也有其他的答案了。一個重要的東西,運營商的優先事項。 andor之前評估。所以第二條if語句實際上說:「如果A或(B和C)」,這意味着「如果A是真的,或者如果B都是真的,那就這麼做」。 以另一種方式:True or True and False讓你True,同時(True or True) and False讓你False