r/CodingHelp • u/ya_hate_2_c_it • Jul 13 '21
[Python] simple python. don’t know what i’m doing wrong :(
43
u/Third_D3gree Jul 14 '21
Yeah, you're really close, you just need to change the indentation of the else statements.
Each "else" should be indented as far over as the "if" that it's matching.
17
4
u/jaypeejay Jul 14 '21
Dang, I don’t know python. That’s crazy that you do it like that. I was about to say it needs to be elsif..
5
u/Third_D3gree Jul 14 '21
You could also do it with elif if you restructured it a bit:
if year % 400 is 0: print("Leap year") elif year % 100 is 0: print("Not leap year") elif year % 4 is 0: print("Leap year") else: print("Not leap year")-6
u/arf20__ Jul 14 '21
This is why python sucks
2
u/LeonTranter Jul 14 '21
Let me guess, languages forcing you to match curly braces is fine, but forcing you to match indents is not? Because reasons?
-2
-5
u/arf20__ Jul 14 '21
Actually because curly braces are just superior
2
7
u/kd4444 Jul 14 '21 edited Jul 14 '21
Is a leap year not always every 4 years? What are the 100 and 400 tests for?
Edit: oh! It’s not as simple as every 4 years.
2
Jul 14 '21
Leap years are divisible by 4 and 400, but not 100.
3
1
u/kd4444 Jul 14 '21
They can be divisible by 100, like 2000 was a leap year - but if they are divisible by 100 they must also be divisible by 400 to be a leap year. So 1900 was not a leap year. I was very curious about this yesterday!
3
u/iheartsushi7201 Jul 14 '21
The indentation is the issue as others have mentioned. I’d recommend using a single if statement with ‘and’ between the logic. When you use parenthesis around the whole statement, you can have these each on a separate line, indented nicely so it’s still legible.
3
Jul 14 '21
About the code, you need to fix your indentation.
This is not a good way to write a leap year code - although good enough for a beginner. It works, but there is just too many if-else. You can follow the one that I have written:
if (year%4==0 and year%100!=0 or year%400==0):
print("{} is a leap year".format(year))
else:
print("{} is not a leap year".format(year))The basic idea is that a leap year is divisible by 4 and 400, but not 100.
Edit: I am assuming that you know what AND and OR operations are? You can search via Google and Bing to understand more.
3
u/CenturiesAgo Jul 14 '21
A slight change if I may
year = 2010
out = "{0} is not a leap year"
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
out = "{0} is a leap year"
print(out,format(year))2
2
1
u/currawong_ Jul 14 '21
Try drawing this as a flow chart and then match your indentation to the control flow branches.
1
Jul 14 '21
your tabs are wrong. You need to indent your else statements and the statements below them in descending order.
1
u/sericsheon Jul 14 '21
You gotta align your else with if Python unlike c# or c gives a lot of fucks about indentation.
1
Jul 14 '21
Yes! Python relies on intentation for syntax analysis. It's mandatory and not only good practice to have it consistent in Python. Even tabs vs spaces will give you issues.
1
1
u/DanBoiii182 Jul 14 '21
First of all you got the indentation of the else statements wrong and you also need to change those else statements into if else statements (Google on the syntax for those I can't remember what the exact syntax is)
1
u/cbb692 Jul 14 '21 edited Jul 14 '21
The problem is you're using too many lines:
print(f'{year} is {"not " if year % 400 and not year % 100 or year % 4 else ""}a leap year.')
/s
In all seriousness, indentation is definitely a struggle for some people to wrap their head around. Take solace in the fact you had a good understanding on how to approach the problem, even if your execution had some minor hiccups. Not forgetting to hit tab is a much simpler thing to fix than a mindset that does not want to solve problems.
1
0
6
u/ya_hate_2_c_it Jul 13 '21
i’m almost positive it’s the else but i don’t know what i’m doing wrong with it