Python Forum
Error Syntax -HackerRank challenge - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error Syntax -HackerRank challenge (/thread-21243.html)



Error Syntax -HackerRank challenge - RavCOder - Sep-20-2019

Hi everyone,
I'm beginner Python coder and I'm trying the challenges on HackerRank.
I'm doing "Learn to code in 30 days " and I'm on 3 day.
this is the link :link

The compiler say that I have an error syntax on 20 line where there is else condition, but I don't know how to resolve it.
I was also wondering if my code is right or if there are other errors that I can't see.
Can you help me ?
Thank you and greetings,
RavCOder
import math
import os
import random
import re
import sys



if __name__ == '__main__':
    N = int(input())
if N % 2 :
    print("Weird")
elif N % 1 and  N in range (2,5):
    print("Not Weird")
elif N % 1 and N in range (6, 20):
    print("Weird")

else N % 1 and  N > 20:
    print("Not Weird")



RE: Error Syntax -HackerRank challenge - axju - Sep-20-2019

Change else to elif in line 18


RE: Error Syntax -HackerRank challenge - RavCOder - Sep-20-2019

Thanks It works, but I can ask why with else it doesn't work while with elif works


RE: Error Syntax -HackerRank challenge - perfringo - Sep-20-2019

'else' is else as in spoken language. This means that no conditions.


RE: Error Syntax -HackerRank challenge - ichabod801 - Sep-20-2019

The else statement is for if all the other tests in the if/elif test fail. It is basically short hand for elif True:, except it also signals that there can be no more elifs. If you want a condition, you use elif.


RE: Error Syntax -HackerRank challenge - perfringo - Sep-20-2019

If I look at the conditions (nicely obfuscated):

Quote: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

1 <= n <= 100

There should be only one 'if' and else, also no need to be defensive about negative values or zero:

if bool(n % 2) or 6 <= n <= 20: 
    print('Weird') 
else: 
    print('Not weird')



RE: Error Syntax -HackerRank challenge - RavCOder - Sep-20-2019

Surely there is a simpler way with a few lines rather than using as many lines as I did, but I have not yet learned to "simplify" my code.
I'm still learning and if there are any specific things I can use or learn to make my code more readable and with a few lines, I'll follow it.


RE: Error Syntax -HackerRank challenge - perfringo - Sep-20-2019

This is not about “fewer lines”, it’s about understanding the constraints/ conditions. Only relevant conditions should be used and this is “thinking before coding”. Code is just expression of your idea/ understanding.


RE: Error Syntax -HackerRank challenge - RavCOder - Sep-20-2019

You're right, forgive me I didn't mean "write a few lines", I expressed myself badly. Surely "thinking before coding" is important, that's why I'm trying to learn not only the basics but also to build a logical thought to solve coding problems.


RE: Error Syntax -HackerRank challenge - perfringo - Sep-20-2019

No problem. If you look at the conditions - they seem complicated and there are so many of them. But it’s actually: “all odd numbers and numbers in range 6...20 are weird, everything else is not weird”. Isn’t it much easier and simpler?

All assignments try to obfuscate the conditions and mislead you into assuming things. Your task is find relevant stuff and ignore noise.

Happy coding!