Python if statement doesn't work as expected

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

    The way you wrote your if statements is wrong. You tell python to check if fleechance equals 1 is true or if 2 is true. A non-zero integer always means true in a condition. You should wrote :

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

提交回复
热议问题