Python If-Else - Hacker Rank Solution

Python 

python is the most famous programming language. as

  • It is very easy to use and learns because of easy syntax therefore it is easy to execute and write.
  • It has a huge amount of Python Libraries and Frameworks.
  • Python is used in Big data, Machine Learning, Cloud Computing, and many others.
  • Python is always the first preference of a programmer to start their programming career.
  • It is one of the most flexible programming language.
  • Python is also widely used in automation works.

Python coding questions

Python If-Else

Task 
Given an integer, , perform the following conditional actions:
  • n is odd, print Weird
  • n is even and in the inclusive range of  to , print Not Weird
  • n is even and in the inclusive range of  to , print Weird
  • n is even and greater than , print Not Weird
Input Format
A single line containing a positive integer, .
Constraints
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.
this question can be solved in both python 2 and python 3 in various way one of them is stated below:
Python If-Else - Hacker Rank Solution
Python 2
 n = int(raw_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"

Python 3

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")




Post a Comment

0 Comments