使用赋值运算符时,我没有得到正确的输出

时间:2018-01-17 12:41:05

标签: python-3.x

给定一个整数,执行以下条件操作:如果是奇数,则打印奇怪如果是偶数且在包含范围内,则打印不奇怪如果是偶数且在包含范围内,则打印奇怪如果是偶数并且大于,打印Not Weird

if __name__ == '__main__':
    n = int(input("Enter a number:"))

    if n % 2 != 0:
        print("Weird")

    elif n % 2 == 0:
        for n in range(2,5):
            print("Not Weird")

    elif n % 2 == 0:
        for n in range(6,20):
            print("Weird")

    elif n% 2 == 0 && (n > 20):

            print("Not Weird")

1 个答案:

答案 0 :(得分:0)

for是一个循环,range()是一个生成器(实际上是immutable sequence),它不会按照您的想法执行。您需要查看更多ifelif语句,例如:

if __name__ == '__main__':
    n = int(input("Enter a number:"))

    if n % 2 != 0:
        print("Weird")
    else:
        if 2 <= n <= 5:
            print("Not Weird")
        elif 6 <= n <= 20:
            print("Weird")
        elif n > 20:
            print("Not Weird")
        else:
            print("n = 0 or even-and-negative")