Python If-Else | HackerRank Solution

Hello coders, today we will be solving Python If-Else Hacker Rank solution.

Python If-Else - Hacker Rank Solution

Contents

Problem

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 is even and in the inclusive range of 6 to 20, print Weird
  • If is even greater than 20, print Not Weird

Input Format

A single line containing a positive integer, n.

Constraints

 n  100

Output Format

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

Sample Input

3

Sample Output

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 is even, so it is not weird. 

Solution – Python If-Else Hacker Rank Solution

n = int(input())
if n % 2 == 1:
    print("Weird")
elif n % 2 == 0 and 2 <= n <= 5:
    print("Not Weird")
elif n % 2 == 0 and 6 <= n <= 20:
    print("Weird")
else:
    print("Not Weird")            

Disclaimer: The above Problem (Python If-Else) is generated by Hacker Rank but the Solution is provided by CodingBroz.

7 thoughts on “Python If-Else | HackerRank Solution”

  1. kalpana pandey

    sir can i code like this….i’m a beginner incoding
    n= int(input())
    if n%2==1:
    print(‘weird’)
    elif n%2==0 and n>20:
    print(‘not weird’)
    elif n%2==0 and n==range(2,5):
    print(‘not weird’)
    elif n%2==0 and n==range(6,20):
    print(‘not weird’)

  2. n= int(input())
    if n%2==1:
    print(‘weird’)
    elif n%2==0 and n>20:
    print(‘not weird’)
    elif n%2==0 and n in range(2,5):
    print(‘not weird’)
    elif n%2==0 and n in range(6,20):
    print(‘not weird’)

  3. can anybody tell me what is wrong with my code
    N = int(input().strip())
    if N%2==0:
    if N>20:
    print(“not weird”)
    elif N=6:
    print(“weird”)
    elif N>=2 and N<=5:
    print("not weird")
    else:
    print("weird")

    1. 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 greater than 20, print Not Weird

      Bro its nothing:
      If n is even and in the inclusive range of 6 to 20, print Weird here I think you made a mistake it says that the range between(6 and 20)

      Then here (elif N>=2 and N<=5:)

      print("weird") you forgot to mention (odd or even)

  4. jananayaygan

    def weird(n):
    if n%2!=0:
    print(“Weird”)
    elif n%2==0 and n>20:
    print(“Not Weird”)
    elif n%2==0 and n in range(2,5):
    print(“Not Weird”)
    elif n==20:
    print(“Weird”)
    elif n%2==0 and n in range(6,20):
    print(“Weird”)

    if __name__ == ‘__main__’:
    n = int(input().strip())
    weird(n)

  5. Today i strat my journey with this code. i am a beginner in coding.. pls help me to learn

    n=int(input())
    if n%2!=0:
    print(“weird”)
    elif n%2==0 :
    if n>20 :
    print(“not weird”)
    elif 2<n and n<5 :
    print("not weird")
    elif 620:
    print(“weird”)

Leave a Comment

Your email address will not be published. Required fields are marked *