If和多个整数的麻烦

时间:2017-04-07 14:32:58

标签: python python-3.x

尝试修复一些代码:

function():
    answer = int(input("Write a number"))
    if answer == 101:
         print("You found a secret!")
         do_stuff()
    elif answer >= 5 and answer <= 100 and answer >= 102:
        print("This is not a valid option!")
        function()
    else:
        continue with other inputs

特别是elif的答案&gt; = 5(等等)不能正常工作,它只是转移到其他:其他输入,我不知道如何解决它。

2 个答案:

答案 0 :(得分:3)

永远不会出现这种情况
answer >= 5 and answer <= 100 and answer >= 102

!我想你可能意味着

answer >= 5 and answer <= 100 or answer >= 102

。或者如何:

5 <= answer <= 100 or 102 <= answer

答案 1 :(得分:1)

对于你的第一个elif语句,你的逻辑是不正确的。 python和语句意味着所有条件必须为true才能执行。然而,你写的答案必须大于5,小于100,大于102.这没有意义,因为数字不能大于100且小于100.你需要的是一个或者语句:

elif answer >= 5 and answer <= 100 or answer >= 102: