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 is odd and odd numbers are weird, so we print Weird.
Sample Input 1
24
Sample Output 1:
Not Weird
Explanation 1:
n = 24n > 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.
its not
work
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
Where is 1<=n<=100 constraint used?
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
Please explain 9 and 10 line.
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.
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.
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
#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")
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")