Python if statement doesn't work as expected

后端 未结 5 1836
没有蜡笔的小新
没有蜡笔的小新 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

    The if statement is working as designed, the problem is that order of operations is causing this code to do something other than that you want.

    The easiest fix would be to say:

    if fleechance == 1 or fleechance == 2:
        print "You failed to run away!"
    elif fleechance == 3 or fleechance == 4:
        print "You got away safely!"
    

提交回复
热议问题