简体   繁体   English

Python游戏if语句执行

[英]Python Game if statement executing

I'm working on the Python Pig Game as a hobby. 我正在业余爱好Python Pig Game。 My specific question is in regards to the second if statement. 我的具体问题是关于第二个if语句。 Why does it run when in the previous if statement I've set player1roll = 'End Turn' ? 为什么在上一个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

It's due to the operator precedence. 这是由于运算符的优先级。 AND binds tighter than OR, so your expression is evaluated as: AND比OR绑定更紧密,因此您的表达式的求值为:

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

which is basically an OR operation of True and False if dice1roll equals 1 如果dice1roll等于1,则基本上是TrueFalse运算

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

In this: 在此:

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

If dice1roll == 1 is true, the entire statement passes due to the order of operations. 如果dice1roll == 1为true,则由于操作顺序而使整个语句通过。 Take a look at these groupings: 看一下这些分组:

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

Use the following instead: 请改用以下内容:

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

I suppose "the second if statement" you mention is this one: 我想您提到的“第二个if语句”就是这个:

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

the difference between this one and the one before is that the first one says "if A and B, do this", while the second says "if A or B and C , do this". 这与之前的一个的区别在于,第一个说“如果A B,请执行此操作”,而第二个说“如果A B 和C ,请执行此操作”。

The "and" operator will make the statement true only if both A and B are true (if one of them is false, then the statement is false). 当A和B都为true时,“ and”运算符才会使语句为true(如果其中一个为false,则该语句为false)。 The "or" operator will make the statement true only if A or B is true (both can be true, but if only one is true then the statement is true). 仅当A B为true时,“或”运算符才会使语句为true(两者都可以为true,但是如果只有一个为true,则该语句为true)。 So, while you need A and B to be true for an "and" statement to be true, you only either A or B (or both) to make an statement with the "or" logical operator to be true. 因此,虽然您需要A和B为“ and”语句为真,但您只有A或B(或两者)才能使“ or”逻辑运算符为真。

Have a look at the documentation: https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not (note: this is for Python 2.7, but I think the same works in Python 3) 看一下文档: https : //docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not (注意:这是针对Python 2.7的,但是我认为在Python 3)

Edit: I guess I was slow, there are other answers now. 编辑:我想我很慢,现在还有其他答案。 One points to something important, the operator precedence. 一个指向重要的东西,即运算符的优先级。 and evaluates before or . andor之前评估。 So the second if statement actually says: "if A or (B and C)", which means "if A is true, or if B and C are both true, do this". 因此,第二个if语句实际上说:“如果A或(B和C)”,这意味着“如果A为真,或者B C都为真,则执行此操作”。 Put in another way: True or True and False gets you True , meanwhile (True or True) and False gets you False 换一种说法: 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