Python if statement doesn't work as expected

后端 未结 5 1851
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 22:55

I currently have the code:

fleechance = random.randrange(1,5)
print fleechance
if fleechance == 1 or 2:
    print \"You failed to run away!\"
elif fleechance         


        
5条回答
  •  鱼传尺愫
    2020-12-11 23:42

    Because you're not asking whether fleechance is 1 or fleechance is 2; you're asking whether

    1. fleechance is 1, or
    2. 2 is non-zero.

    Of course, that second part of the condition is always true. Try

    if fleechance == 1 or fleechance == 2:
        ...
    

提交回复
热议问题