Python If-Else HackerRank Solution with Explanation

Python If-Else HackerRank Solution with Explanation

Hello Friends, How are you? Today I am going to solve the HackerRank Python if-else Problem with a very easy explanation. This is the 2nd problem of Python on HackerRank. In this article, you will get more than one approach to solve this problem. So let's start-

{tocify} $title={Table of Contents}

HackerRank Python If-Else 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:

Python If-Else HackerRank Solution with Explanation
Source: Wikipedia

HackerRank Python If-Else Problem Statement

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 and odd numbers so it is Not Weird.

Python If-Else HackerRank Solution

Approach I:

#!/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 Problem (Python If-Else) is generated by Hackerrank but the Solution is Provided by Code Solution. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

Also Check: 


I hope you have understood the solution to the Python If-Else HackerRank Problem. The above solution will pass all the test cases. Now visit Python If-Else HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post