Python If-Else – HackerRank Solution

Python If-Else

Given an integer, n, perform the following conditional actions:

• If n is odd, print `Weird`
• If n is even and in the inclusive range of 2 to 5, print `Not Weird`
• If n is even and in the inclusive range of 6 to 20 , print `Weird`
• If n is even and greater than 20, print `Not Weird`

Input Format

A single line containing a positive integer,n.

Constraints

• 1<=n<=100

Output Format

Print `Weird` if the number is weird. Otherwise, print `Not Weird`.

Sample Input 0

```3
```

Sample Output 0

```Weird
```

Explanation 0

n=3
n is odd and odd numbers are weird, so print `Weird`.

Sample Input 1

```24
```

Sample Output 1

```Not Weird
```

Explanation 1

n =24
n > 20 and n is even, so it is not weird.

Code:

``` n = int(input().strip())
if n%2!=0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
else:
print("Not Weird")```

This code is a solution to the “Python If-Else” problem on HackerRank.

The program takes an integer input n from the user using the input() function, and then removes any leading or trailing whitespace characters using the strip() function.

The first if statement checks if n is odd (n % 2 != 0). If n is odd, the program prints “Weird” because the problem statement requires odd numbers to be classified as “Weird”.

If n is even, the program proceeds to the second if statement. Here, the program checks if n is within the range of 2 to 6 (inclusive) using the range() function. If n is within this range, the program prints “Not Weird” because even numbers within this range are classified as “Not Weird” according to the problem statement.

If n is not within the range of 2 to 6, the program proceeds to the next elif statement. Here, the program checks if n is within the range of 6 to 20 (inclusive) using the range() function. If n is within this range, the program prints “Weird” because even numbers within this range are classified as “Weird” according to the problem statement.

If n is not within the range of 2 to 20, the program executes the final else statement and prints “Not Weird” because all remaining even numbers are classified as “Not Weird” according to the problem statement.

In summary, this program takes an integer input n, checks if it is odd or even, and then classifies it as “Weird” or “Not Weird” based on the ranges specified in the problem statement.

Disclaimer: These problems are originally created and published by HackerRank we only provide solutions to those problems. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.