If Statement Always True (String)

北战南征 提交于 2021-02-17 05:57:04

问题


I have here a rather simple rock, paper, scissors program where I am having some trouble with if statements. For some reason, when I enter rock, paper, or scissors (True Values), the program always performs

if 'rock' or 'paper' or 'scissors' not in player:
   print("That is not how you play rock, paper, scissors!")

for some reason. The complete program is as below.


computer = ['rock', 'paper', 'scissors']

com_num = randint(0,2)

com_sel = computer[com_num]

player = input('Rock, paper, scissors GO! ')
player = player.lower()

if 'rock' or 'paper' or 'scissors' not in player:
    print("That is not how you play rock, paper, scissors!")

if player == 'rock' or 'paper' or 'scissors':    
    #win
    if player == 'rock' and com_sel == 'scissors':
        print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
    if player == "paper" and com_sel == "rock":
        print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
    if player == 'scissors' and com_sel == 'paper':
        print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')

    #draw
    if player == com_sel:
        print('It\'s a draw!')
        
    #lose
    if player == 'rock' and com_sel == 'paper':
        print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
    if player == 'paper' and com_sel == 'scissors':
        print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
    if player == 'scissors' and com_sel == 'rock':
        print('You lose.', com_sel.title(), "beats", player, '!', sep = '')```

回答1:


The conditions in if are wrong.

Consider the if statement with parentheses:

if ('rock') or ('paper') or ('scissors' not in player):

It will always return True because rock will always be true.

You need to swap conditions' operands

if player not in computer:

After this swap, this line becomes irrelevant (and also its conditions are wrong) You need to remove it:

if player == 'rock' or 'paper' or 'scissors': 



回答2:


Look at this page with Python's operator precedences (the order in which they apply):

https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

You can see that not in is listed higher than or which means that it is evaluated first. Thus you can rewrite your if statement as:

if 'rock' or 'paper' or ('scissors' not in player): ...

Now we see that you really have an or of three things. The strings are not empty and thus the first 'rock' already evaluates to true so the the whole thing is always true.




回答3:


To breakdown your statement,

if 'rock' or 'paper' or 'scissors' not in player:

Suppose, player = 'scissors'. This condition would be evaluated as,

('rock') or ('paper') or ('scissors' not in player)

which again evaluates to,

True or True or False

Hence evaluating to True always because string(Eg 'rock') always evaluates to True and ignoring others because of the OR(Any one True to be True). So whatever you put in player doesn't matter.

CORRECT CONDITION

if player not in ['rock', 'paper', 'scissors']:

This statement checks if player is not in the given list.




回答4:


if player not in {'rock', 'paper', 'scissors'}:
    print("That is not how you play rock, paper, scissors!")
...


来源:https://stackoverflow.com/questions/63987341/if-statement-always-true-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!