Python If-Else HackerRank Solution

Problem

Check Tutorial tab to know how to to solve.

Task

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 nor weird.

Solution to HackerRank If-Else Solution In Python

In this problem, we are to identify if a given number is odd, or even between the range of 2 and 5, 6 and 20, and greater then 20.

If the number is odd, print Weird, if it is even and within the range of 2 and 5, print Not Weird, and if it is even and within the range 6 and 20, print Weird, if it is even but greater than 20, print Not Weird.

Python: if-else hackerrank solution
Result after the below code was ran on the editor.
if __name__ == '__main__':    n = int(input())    if n%2 == 1:        print('Weird')    elif (n in range(2, 6)) and (n%2 == 0):        print('Not Weird')    elif (n in range(6, 21)) and (n%2 == 0):        print('Weird')    else:        print('Not Weird')

Thanks for reading

Comments