简体   繁体   中英

Why the output is printed “Not Wierd” When the input is 18?

Why the output is printed "Not Wierd" When the input is 18?

if __name__ == '__main__':
    n = int(input().strip())
    if(n%2==0):
        if(range(2,5)):
            print("Not Weird")
        elif(range(6,20)):
            print("Weird")
        elif(n>20):
            print("Not Weird")
    else:
        print("Weird")

Use n in range(...) to see if n is within range.

Issue is with your use or range() , which returns an iterable, which is a truthy value (if it's not empty), hence your first conditional always being true.

Update it so you are checking if your number is inside the iterable returned by range()

...
if(n in range(2,5)):
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM