Home 02. Python If-Else
Post
Cancel

02. Python If-Else

Problem: Python If-Else

Solution 1: Using nested if-else statements

1
2
3
4
5
6
7
8
9
10
n = int(input().strip())
if n%2==1:
        print('Weird')
else:
    if 2<=n<=5:
        print('Not Weird')
    elif 6<=n<=20:
        print('Weird')
    else:
        print('Not Weird')

Solution 2: Using if-else statements

1
2
3
4
5
6
7
8
9
10
if __name__ == '__main__':
    n = int(input().strip())
    if n % 2 != 0:
        print("Weird")
    elif n % 2 == 0 and n in range(2, 6):
        print("Not Weird")
    elif n % 2 == 0 and n in range(6, 21):
        print("Weird")
    elif n % 2 == 0 and n > 20:
        print("Not Weird")

Solution 3: Using multiple conditions in one if statements

1
2
3
4
5
6
if __name__ == '__main__':
    n = int(input().strip())
    if n%2 or 6 <= n <= 20:
        print('Weird')
    else:
        print('Not Weird') 

Solution 4: One liner solution

1
2
n = int(input())
print('Weird' if n%2 or n in range(6, 21) else 'Not Weird')
This post is licensed under CC BY 4.0 by the author.

01. Say "Hello, World!" With Python

03. Arithmetic Operators