Explore

Close
Hackerrank Solutions Share

Python If Else HackerRank Solution

Python If Else HackerRank Solution

Looking for python if else hackerrank solution, then you are at the right place today in this tutorial. I will share with you hackerrank if else solution in python programming, so follow this tutorial till the end.

Hackerrank is a platform with coding challenges where you can take them and improve your programming skills. The challenge problem we are solving is created by hackerrank.

Python If Else Hackerrank Problem

Ask the user for integer input and store it in variable n and then using if else statements check the following conditions:

The Complete Python Programming Boo...
The Complete Python Programming Bootcamp
  • 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

To check the full question visit here: Python If Else HackerRank. Now let’s see how to do this is python programming.

Python If Else Hackerrank Solution Code


n = int(input("Enter a number: "))
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")            

Above is the hackerrank if else solution in python. After running this program you will asked to enter a number and then it will print weird or not weird, Below is an example output.


6
Weird

Python If Else Hackerrank Solution Code Explaination

First we ask the user to enter a number we convert the number to int using int() method. Next we check the conditions using if else.


if n % 2 == 1:
    print("Weird")     

The first condtion is to print weird if the number entered is odd as you can see in the above code we find the remainder of the number when divide by 2 and check if it is equal to 1 if it is equal to 1 then it is a odd number and it will print weird.


elif n % 2 == 0 and 2 <= n <= 5:
    print("Not Weird")          

The second condition is to print not weird when user entered number is even and if the number entered is between 2 to 5. As you can see in the above code we first check if the number is even by dividing by 2 and if the remainder is 0 it is even and also we check if the number is between 2 and 5.


elif n % 2 == 0 and 6 <= n <= 20:
    print("Weird")         

The third condition is to print weird when user entered number is even and if the number entered is between 6 to 20. As you can see in the above code we first check if the number is even by dividing by 2 and if the remainder is 0 it is even and also we check if the number is between 6 and 20.


else:
    print("Not Weird")         

The last condition will print weird if all the 3 condition becomes false that is what the job of else is and this is hackerrank if else program in python.

Summary

This was the python if else hackerrank solution. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.

Here are some more python hackerrank tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂