Posts: 69
Threads: 20
Joined: Sep 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")
Posts: 5
Threads: 2
Joined: Sep 2019
Change else to elif in line 18
Posts: 69
Threads: 20
Joined: Sep 2019
Thanks It works, but I can ask why with else it doesn't work while with elif works
Posts: 1,950
Threads: 8
Joined: Jun 2018
'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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Sep-20-2019, 02:08 PM
(This post was last modified: Sep-20-2019, 02:09 PM by perfringo.)
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.
Posts: 69
Threads: 20
Joined: Sep 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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 69
Threads: 20
Joined: Sep 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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
|