If Statement Always True (String)

后端 未结 4 1512
悲&欢浪女
悲&欢浪女 2021-01-28 17:32

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)

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 18:07

    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.

提交回复
热议问题