Python If-Else - Hacker Rank Solution


Python If-Else - Hacker Rank Solution



Problem


Tutorial
In this challenge, we test your knowledge of using if-else conditional statements to automate decision-making processes. An if-else statement has the following logical flow:



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 we print Weird.

Sample Input 1

24

Sample Output 1:

Not Weird

Explanation 1:

n = 24
n > 24 and n is even, so it isn't weird. Thus, we print Not Weird.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/python
#Python If-Else - Hacker Rank Solution
import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(raw_input().strip())
    # Python If-Else - Hacker Rank Solution START
    if n%2 != 0:
        print("Weird")
    else :
        if(n>=2 and n<=5):
            print("Not Weird")
        elif(n>=6 and n<=20):
            print("Weird")
        elif(n>20):
            print("Not Weird")
    # Python If-Else - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
10 Comments
  • Unknown
    Unknown Friday, November 06, 2020

    its not
    work

    • Unknown
      Unknown Friday, June 04, 2021

      elif(n<=2 and n<=5):
      and
      elif(n<=6 and n<=20):

      is the correct code Bro
      only change greater than to less than

  • SuperDude
    SuperDude Thursday, January 07, 2021

    Where is 1<=n<=100 constraint used?

    • CodeWorld19
      CodeWorld19 Thursday, January 07, 2021

      This is a constraint which show you the limitation of the value n is not lesser than 1 and not greater than 100. And the n is input gives from user

  • Unknown
    Unknown Monday, April 05, 2021

    Please explain 9 and 10 line.

    • CodeWorld19
      CodeWorld19 Wednesday, April 07, 2021

      line no 9 is main function -> which is the execution point for a program file
      line no 10 is -> given input from user, don't worry about the strip() it is used to perform string operation to clear right and left space of the string , and its not needed hear it's my mistake, but its not affect solution.

  • Unknown
    Unknown Sunday, June 13, 2021

    if n%2 != 0 :
    print('Weird')
    else:
    if(n>=2) and (n<=5):
    print('Not Weird')
    elif(n>=6) and (n<=20):
    print('Weird')
    elif(n>20):
    print('Not Weird')



    Working for all case.

  • Unknown
    Unknown Saturday, July 24, 2021

    if __name__ == '__main__':
    n = int(input().strip())
    if n%2 == 0:
    if (2<=n<=5 and n>20):
    print('Not Weird')
    elif (6<=n and n<=20):
    print('Weird')
    elif (n>20) :
    print('Not Weird')
    else:
    print('Weird')
    what is the problem with this code

  • Unknown
    Unknown Tuesday, August 10, 2021

    #me ravi(akshay) gupta at linkedin

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

  • Unknown
    Unknown Thursday, November 18, 2021

    if __name__ == '__main__':
    n = int(input().strip())
    if n%2 != 0:
    print("Weird")
    else :
    if(n>=2 and n<=5):
    print("Not Weird")
    elif(n>=6 and n<=20):
    print("Weird")
    elif(n>20):
    print("Not Weird")

Add Comment
comment url