Python If-Else - Hacker Rank Solution
Input Format
A single line containing a positive integer, .
Constraints
Output Format
Print
Weird
if the number is weird; otherwise, print Not Weird
.
Sample Input 0
3
Sample Output 0
Weird
Sample Input 1
24
Sample Output 1
Not Weird
Explanation
Sample Case 0:
is odd and odd numbers are weird, so we print
is odd and odd numbers are weird, so we print
Weird
.
Sample Case 1:
and is even, so it isn't weird. Thus, we print
and is even, so it isn't weird. Thus, we print
Not Weird
.Python If-Else - Hacker Rank Solution
Problem Setter's code :
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")
if N %2 != 0 or 5<N<21:
ReplyDeleteprint ("Weird")
else:
print ("Not Weird")
n = int(raw_input())
ReplyDeleteif n%2==0 and ((2 <= n <=5) or (n>20)):
print("Not Weird")
if (n%2==1 or (n%2==0 and (6 <= n <=20))):
print("Weird")
Its for python 2
DeleteTrue!
DeleteGreat thala!
DeleteThis program is sowing compilation error.
DeleteCorrect program is mentioned below
n=int(input())
if((n%2!=0)or(n%2==0 and 6<=n<=20)):
print("Weird")
elif((n%2==0 and 2<=n<=5)or(n%2==0 and n>20)):
print("Not Weird")
Y we hve to "n=int(input())"
Deleteit s nothing but user input
Deletewithout it you can't print the value
Deletewhat is wrong wit my code ?
Deleten=int(input())
if n%2!==0 or (620:
print("Not Weird")
n=int(input())
Deleteif n % 2!= 0 or 620:
print("Not Weird")
Above is write way
This comment has been removed by the author.
ReplyDeleten=int(input("enter your odd or even number: "))
ReplyDeleteif 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")
thank you very much
DeleteWhy its showing weird for n=24
ReplyDeleteyour just on point
ReplyDeleteThanks a lot for this information. I loves to read this blog.
ReplyDelete
ReplyDelete#what is wrong with this code ?
n=input()
m=int(n)
if m%2!=0:
print("Weird")
elif m%2==0:
if(2<=m<=5):
print("Not Weird")
elif m%2==0:
if (6<=m<=20):
print("Weird")
elif m%2==0:
if (m>20):
print("Not Weird")
Bro last else varanum bro apatha correct
Deleteyour code may be correct but
Deletewhy u take modulous of 2 more than one time .
#whats wrong with my code please help me out..!! Thanks in Advance!!
ReplyDeleten = int(input("enter odd or even number"))
if n % 2 == 1:
print("n is weird")
else:
if n >= 2 and n <= 5:
print("n is not weird")
elif n % 2 == 0 and 6 <= n <= 20:
print("n is weird")
elif n >= 20:
print("n is not weird")
n = int(input())
Deleteif n % 2 == 1:
print("Weird")
else:
if n >= 2 and n <= 5:
print("Not Weird")
elif n % 2 == 0 and 6 <= n <= 20:
print("Weird")
elif n >= 20:
print("Not Weird")
This is working aa
DeleteThey have imported re i.e, regular expression module so that must have something to do with the code
ReplyDeleteyes you are right!!! but do you have any idea because if I remove than last__name__ = __ main__ this solution works but I don't know what to do now.
DeleteWhy this code is not run?
ReplyDeleten = 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")
Im having the same issue with this
DeleteMy code is same as that code but it showing indentation error. please tell me what is the fault in this code.
Deleteif n%2!=0:
ReplyDeleteprint("Weird")
elif 2<=n<=5:
print("Not Weird")
elif 6<=n<=20:
print("Weird")
elif n>20:
print("Not Weird")
3
Deleten<
DeletePlease remove that default if statement.It'll work then.
ReplyDeleteCan anyone check why 1 test case out of 8 is failing.
ReplyDeletedef calculateGrade(students_marks):
arr =students_marks
result =[]
for row in arr:
avg=0
number=0
for col in row:
number=number+col
#print(number)
avg=number/len(row)
if avg >=90:
result.append('A+')
elif avg >=80 and avg <90:
result.append('A')
elif avg >=70 and avg <80:
result.append('B')
elif avg>=60 and avg <70:
result.append('C')
elif avg>50 and avg<60:
result.append('D')
else:
result.append('F')
return result
for java?
ReplyDeleten = int(input())
ReplyDeleteif (n%2 == 1):
print("Weird")
elif (n%2 ==0) and ((n >= 2) and (n <= 5)):
print("Not Weird")
elif (n%2 ==0) and ((n <= 6) and (n <= 20)):
print("Weird")
else:
print("Not Weird")
This is for Python 3
Deletei =int(input())
ReplyDeleteif(i % 2)==0:
if(2 <= i <= 5):
print("Not Weird")
if( 6<= i <= 20):
print("Weird")
if( 20 <=i):
print("Not Weird")
else:
print("Weird")
there is one test case wrong
what is it
this is for python 3
Deletewhat is wrong in this code and also tell the other ways to assign the inclusive range
ReplyDeleteif __name__ == '__main__':
n = int(input().strip())
if(n%2!=0):
print('Weird')
elif(n%2==0, n in range(2,5)):
print('Not Weird')
elif(n%2==0, n in range(6,20)):
print('Weird')
else:
print('Not Weird')
n = int(input())
ReplyDeleteif n % 2 !=0:
print("Weird")
else:
if n % 2==0 and n>=2 and n>=5:
print("Not Weird")
elif n % 2==0 and n>=6 and n>=20:
print("Weird")
elif n>20 :
print("Not Weird")
n = input (" ")
ReplyDeleteif n % 2 == 1 and ((520) or (n==1)):
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"
n = input (" ")
Deleteif n % 2 == 1 and ((520) or (n==1)):
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"
n=int(input().strip())
ReplyDeleteif((n%2!=0)or(n%2==0 and 6<=n<=20)):
print("Weird")
elif((n%2==0 and (2<=n<=5)or n>20)):
print("Not Weird")
Why this code gives error???
ReplyDeleten = int(input().strip())
if n % 2 == 1:
print("Weird")
else:
if n in range(2,5) or n >20:
print("Not Wierd")
elif n in range(6,20):
print("Weird")
Thanks
ReplyDeletethank you
ReplyDeleten = int(input())
ReplyDeleteif n % 2 == 0 and n < 5 or n > 20 : #true
print("Not Weird")
if n % 2 != 0 or n % 2 == 0 and n > 5 and n < 20 : #true
print("Weird")
27 gives both answers - can anyone assist?
if n%2!=0:
ReplyDeleteprint('Weird')
elif n%2==0 and n in range(2,5):
print('Not Weird')
elif n%2==0 and n in range(6,21):
print('Weird')
else:
print('Not Weird')
a=int(input())
ReplyDeleteif a%2==0:
if 1<a<6:
print("Not Weird")
elif 5<a<21:
print("Weird")
else:
print("Not Weird")
else:
print("Weird")
# me ravi(akshay) gupta in linkedin
ReplyDeletea=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 n%2!=0:
ReplyDeleteprint('Weird')
elif n%2==0 and 2 <= n >=5:
print('Not Weird')
elif n%2==0 and 6 <=n >=20:
print('Weird')
else:
print('Weird')
n=int(input())
ReplyDeleteif 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 ")
n=int(input())
ReplyDeleteif 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 ")
n=int(input())
ReplyDeleteif(n%2==0):
if(n in range(2,6)): print("Not Weird")
if(n in range(6,21)):print("Weird")
if(n>20):print("Not Weird")
else:print("Weird")
print("enter a no")
ReplyDeletea=int(input())
n=a%2
if n!=0 or n==0 and 6 <= a <= 20:
print("weird")
else:
print("not weird")
nice bro solution
ReplyDeleten = int(input("Enter any number/n"))
ReplyDeleteif n % 2 == 1:
print("Weird")
elif 2 <= n <= 5:
print("Not Weird")
elif 6 <= n <= 20:
print("Weird")
else:
print("Not Weird")
what is wrong with this code
its showing correct output but hackerrank is not accepting it
if __name__ == '__main__':
ReplyDeleten = int(input().strip())
if(n%2!=0):
print("weird")
elif(n%2==0):
if(n in range(2,5)):
print("not weird")
elif(n in range(6,20)):
print(" weird")
if(n>20):
print("not weird")
only test case 0,4 is passed. what is the problem for other test case
Deletei = int(input())
ReplyDeleteif i%2!=0:
print('Weird')
elif i%2==0 and i in range(2,6):
print('Not Weird')
elif i%2==0 and i in range(6,21):
print('Weird')
elif i%2==0 and i>20:
print('Not Weird')
please soeone rectify my code
ReplyDeleten=int(input())
if n%2!==0 or (620:
print("Not Weird")
n = int(input())
ReplyDeleteif n % 4 == 1:
print("Weird")
elif n % 4 == 0 and 2 <= n <= 6:
print("Not Weird")
elif n % 4 == 0 and 8 <= n <= 30:
print("Weird")
else:
print("Not Weird")
https://capbrain.blogspot.com/p/web-stories.html
ReplyDelete