+ 2

Please help solve leap yeah been week my code wen I test gives me what I want but solo is not approving

Guyz please help https://sololearn.com/compiler-playground/cWZO52G8hVEU/?ref=app

20th Mar 2024, 12:49 PM
ramoholokhathu
ramoholokhathu - avatar
22 ответов
+ 6
#If the year is divisible by 4 then it is a leap year if (year % 4 == 0): #If the year is not divisible by 100, it is a leap year. if (year % 100 != 0): print("Leap year") #If the year is divisible by 400, it is a leap year. elif (year % 400 == 0): print("Leap year") #In which these conditions are not met it is because it is not a leap year else: print("Not a leap year") If the first condition is not met, it is not a leap year. else: print("Not a leap year")
20th Mar 2024, 1:00 PM
STE4LPH
STE4LPH - avatar
+ 3
As said earlier, it is a logic error, not a syntax error. While STE4LPH gave you a working code, but it is best to explain your logical flaw, and how it can be fixed. By definition, leap years are every year evenly divisible by 4, except for years evenly divisible by 100, unless the year is also evenly divisible by 400. Your code is checking in the following order, 4, 100 and 400, from the most inclusive to the less inclusive. And year 1900 is not a leap year, but your code considers it is. To work around your problem, you can start by verifying from the less inclusive condition. That is checking from 400, 100 and 4, and the code will become: if year % 400 == 0: print("Leap year") elif year % 100 == 0: print("Not a leap year") elif year % 4 == 0: print("Leap year") else: print("Not a leap year") In the first if statement, 400, 800, 1200, 1600, 2000 and so on are accounted for, and we don't need to worry if they are divisible by 100. Then number divisible by 100 left are 100, 200, 300, 500, 600, 700, 900 and so on. All of them are not leap year. Finally, we deal with what are left behind. If divisible by 4, then it is a leap year, otherwise not. In this pattern, a nested if statement is not necessary, also every condition is intuitive.
21st Mar 2024, 1:22 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Although the logic is incorrect, the syntax is fine. Would you post your code so we can inspect it? You can follow instructions in this link to post your code in Q&A. https://sololearn.com/compiler-playground/Wek0V1MyIR2r/?ref=app
20th Mar 2024, 1:05 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Done bro
20th Mar 2024, 2:43 PM
ramoholokhathu
ramoholokhathu - avatar
+ 1
ramoholokhathu , The logic problem with your code is that the if, elif, and else clauses are mutually exclusive, meaning if one executes, the others are skipped, so every four years, your if clause executes, and the other filters for 100 years and 400 years are ignored. You need to think about alternative logic structures that can consider multiple conditions instead of one. Here's some ready made code. I am also one who usually only gives hints, but I had too much fun creating it 10 minutes ago and had to post it, because it gave me the first legitimate reason to subclass a built-in class (int), which I've wanted to do for a while. [Edit: I realized later my class didn't actually need to inherit from int, so I still haven't come across a legitimate need to subclass a built-in class. Someday.] Look or don't look. It's up to you. https://sololearn.com/compiler-playground/c94u4YI3HH1n/?ref=app
20th Mar 2024, 5:22 PM
Rain
Rain - avatar
+ 1
Rain It is entirely possible without using else. It is the code. https://sololearn.com/compiler-playground/crCtF8cSFV7p/?ref=app
22nd Mar 2024, 4:59 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
STE4LPH , You present here a ready-made code. It is seen more helpful to give hints, so that the asker can work on his issue himself.
20th Mar 2024, 5:03 PM
sandra
sandra - avatar
0
How
20th Mar 2024, 5:05 PM
ramoholokhathu
ramoholokhathu - avatar
0
Am still a newbie
20th Mar 2024, 5:06 PM
ramoholokhathu
ramoholokhathu - avatar
0
sandra What's the point of giving clues, if you need to learn what you should do now is analyze the code why it marks the error so that your learning can be efficient
20th Mar 2024, 5:11 PM
STE4LPH
STE4LPH - avatar
0
I did that been a 4 days that is why I came here for helpSTE4LPH
20th Mar 2024, 5:15 PM
ramoholokhathu
ramoholokhathu - avatar
0
I did try to fix it the way u showed but am still failing the fact well wen I run the code seems working but solo learn doesn't give me the final mark to pass the test :-(
20th Mar 2024, 5:15 PM
ramoholokhathu
ramoholokhathu - avatar
0
ramoholokhathu It's good that you ask for help, I gave you the code explaining how you had to do it so that you don't make the same mistake anymore.
20th Mar 2024, 5:46 PM
STE4LPH
STE4LPH - avatar
0
Thank you Sir STE4LPH
20th Mar 2024, 5:47 PM
ramoholokhathu
ramoholokhathu - avatar
0
Thank you Sir STE4LPH
22nd Mar 2024, 5:20 AM
Mujtaba
0
Wong Hei Ming HAHA Pure context, please provide it instead of lengthening it, explain it directly, let it be done in your own words without using AI to solve it for you.
22nd Mar 2024, 12:31 PM
STE4LPH
STE4LPH - avatar
0
STE4LPH Sorry and I don't get your point. Also, what made you think I used AI to answer that question? Surely your solution help OP pass the test, and everyone can study your code, but you didn't explain how OP code failed the test. And I believe knowing what went wrong is more important than what is right, aka learn from experience.
22nd Mar 2024, 2:04 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming I'm sorry if I offended you by appearing, you are very angry, I hope your ego goes away and you don't take the words out of Chat gpt. It seems that you passed all your courses using chatgpt
22nd Mar 2024, 2:50 PM
STE4LPH
STE4LPH - avatar
0
STE4LPH I'm not angry, and ChatGPT isn't available in my location, so as Google Bard. Well, copilot is available but not in full potential. And I passed all my courses without using any AI. And you still haven't answer my question. What made you feel I used AI? I'm curious about it.
22nd Mar 2024, 3:29 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
We are the same, so don't ignore the comments that have already been chosen accordingly. And explained in the best way so that they can correct their mistake and can move forward and are not stuck without learning the fundamentals.
22nd Mar 2024, 4:08 PM
STE4LPH
STE4LPH - avatar