Python if statement doesn't work as expected

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

    The expression fleechance == 1 or 2 is equivalent to (fleechance == 1) or (2). The number 2 is always considered “true”.

    Try this:

    if fleechance in (1, 2):
    

    EDIT: In your situation (only 2 possibilities), the following will be even better:

    if fleechance <= 2:
        print "You failed to run away!"
    else:
        print "You got away safely!"
    

提交回复
热议问题