Python Forum
Error Syntax -HackerRank challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Syntax -HackerRank challenge
#1
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")
Reply
#2
Change else to elif in line 18
Reply
#3
Thanks It works, but I can ask why with else it doesn't work while with elif works
Reply
#4
'else' is else as in spoken language. This means that no conditions.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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.
Reply
#8
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
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.
Reply
#10
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!
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,160 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 374 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,545 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  PySpark Coding Challenge cpatte7372 4 6,068 Jun-25-2023, 12:56 PM
Last Post: prajwal_0078
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,297 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,243 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 882 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,829 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,348 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020