Issue
I'm new at learning Python and I have stumbled upon interesting site called HackerRank where you progress by solving task. I have stucked on this one: https://www.hackerrank.com/challenges/py-if-else/problem
Although there are several ways to solve this, I have tried to do it with elif statements as shown in description of task but for some reason, I'm failing to solve this because the last line (elif statement) of my code is not working.
Code:
if __name__ == '__main__':
N = int(input())
num = N % 2
if num > 0:
print("Weird")
elif num == 0 and range(6,20):
print("Weird")
elif num == 0 and range(2,5):
print("Not Weird")
elif num == 0 and N > 20:
print("Not Weird")
So everything works until point where checking if number is even and bigger than 20. I have googled and searched and everywhere I looked, this and operator should work but it doesn't in my case. Can anyone help?
Solution
Your use of range(...) is wrong.
if __name__ == '__main__':
N = int(input())
num = N % 2
if num > 0:
print("Weird")
elif num == 0 and 6 <= N <= 20:
...
Just to use your ideia of range:
# The example bellow just work with int numbers and is more slow than the above approach.
elif num == 0 and N in range(2,21):
Answered By - Joao Vitorino
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.