If Statement Always True (String)

后端 未结 4 1519
悲&欢浪女
悲&欢浪女 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 17:52

    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.

提交回复
热议问题