Python If-Else | HackerRank Solution

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

Python If-Else - Hacker Rank Solution

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.

13 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”)

  6. please rectify error: code is working but, automatically it takes 3 input.

    if __name__ == ‘__main__’:
    n = int(input().strip())
    if n % 2 != 0:
    print(‘wired’)
    elif n % 2 == 0 and n==range(2,5):
    print(‘not wired’)
    elif n % 2 == 0 and n==range(6,20):
    print(‘wired’)
    else:
    print(‘not wired’)

  7. 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’)

  8. import math
    import os
    import random
    import re
    import sys
    if __name__==’__main__’:
    n=int(input().strip())
    if (n%2==1):
    print(“Weird”)
    elif ((n%2==0),n in range(2,6)):
    print(“Not Weird”)
    elif ((n%2==0),n in range (6,21)):
    print(“Weird”)
    else:
    print(“Not Weird”)

  9. public static void main(String[] args) {
    int N = scanner.nextInt();
    scanner.skip(“(rn|[nru2028u2029u0085])?”);
    scanner.close();
    if((N%2!=0)||((N%2==0)&&(N<=6 &&N=20)||((N>=2)&&(N<=5)))
    {
    System.out.print("Not Weird");
    }

    }
    }

  10. n = int(input())
    if n % 2 != 0:
    print(“Weird”)
    if n % 2 == 0 :
    if n = 2:
    print(“Not Weird”)
    if n % 2 == 0 :
    if n = 6:
    print(“Weird”)
    if n % 2 ==0 and n > 20:
    print(“Not Wierd”)
    i am gettting error why plz explain 24 , 100 is getting failed but its working in vs code plz help

Leave a Comment

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