Python If-Else with Explanation

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

Source: Wikipedia

Given an integer, n, perform the following conditional actions: 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 A single line containing a positive integer, n. Print Weird if the number is weird. Otherwise, print Not Weird.

3

Sample Output 0 n is odd and odd numbers are weird, so print Weird.

24

Sample Output 1 n > 20 and n is even and odd numbers so it is Not Weird.

#!/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=6 and n20): print("Not Weird") # Python If-Else - Hacker Rank Solution END
No Comments

Sorry, the comment form is closed at this time.