簡體   English   中英

Python游戲if語句執行

[英]Python Game if statement executing

我正在業余愛好Python Pig Game。 我的具體問題是關於第二個if語句。 為什么在上一個if語句中我將player1roll = 'End Turn'設置player1roll = 'End Turn'時,它為什么運行?

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

這是由於運算符的優先級。 AND比OR綁定更緊密,因此您的表達式的求值為:

   if dice1roll == 1 or (dice2roll == 1 and player2roll != 'End Turn'):

如果dice1roll等於1,則基本上是TrueFalse運算

參考: https : //docs.python.org/3.3/reference/expressions.html#operator-precedence

在此:

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

如果dice1roll == 1為true,則由於操作順序而使整個語句通過。 看一下這些分組:

>>> (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':

我想您提到的“第二個if語句”就是這個:

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

這與之前的一個的區別在於,第一個說“如果A B,請執行此操作”,而第二個說“如果A B 和C ,請執行此操作”。

當A和B都為true時,“ and”運算符才會使語句為true(如果其中一個為false,則該語句為false)。 僅當A B為true時,“或”運算符才會使語句為true(兩者都可以為true,但是如果只有一個為true,則該語句為true)。 因此,雖然您需要A和B為“ and”語句為真,但您只有A或B(或兩者)才能使“ or”邏輯運算符為真。

看一下文檔: 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 C都為真,則執行此操作”。 換一種說法: True or True and False使您獲得True ,同時(True or True) and False使您獲得False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM