Python for beginners - mobile app - help

So I downloaded the mobile app “Python for Beginners.” And I’m on a lesson plan, and not sure why my code is wrong, when I’m getting the correct answer.
Some help in explaining why its wrong?

I’m on 23.1 Lesson - else Statements

The project is to build a set of code to decide if a given year input is a Leap year or Not a leap year.
The input year is 1900
And the answer is supposed to be “Not a leap year.”
But, I’m told to try again as there are bugs.

Hi Duncan, and welcome!

Unless you edit your code with photoshop, please don’t post code as a
screen shot or photo. It means we have to re-type your code to run it,
which can lead to new errors, and makes it difficult or impossible for
people who are blind or visually impaired, or reading via the email
interface, to contribute.

Please copy and paste the code as text when posting, and don’t forget to
use the </> button to format it as code thank you.

So I have no idea what your code says, but 2000 actually is a leap year.
If the answer is supposed to be “Not a leap year” then the answer is
simply wrong.

A year is a leap year if:

  • it is divisible by 4;

  • except for years divisible by 100;

  • unless they are also divisible by 400.

So 1900 was not a leap year, but 2000 was; 2100 will not be a leap year,
but 2400 will be.

1 Like

‘if year == year%4:’
This test will always evaluate to False; plugging in your sandbox input gives 1900 == 0.
You just need to test ‘year%4’.

1 Like

Hi Steven,

Thank you for the input on how to share code, I will do that from now on.

As it turned out… I wasn’t supposed to use:
year == (year % 4)

I was supposed to use:
year % 4 == 0

As to why I was supposed to use the latter, I have no idea. Maybe I’m not fully grasping the “%” meaning or use. I know its supposed to be a mathematical problem that results in a “True” result.

8 hours later
I realized that the code:
2000 == (2000%4)
Results in a “False” result, because 2000%4 = 500, so therefore 2000 does not equal 500, and my code would spit out the wrong answer.

Then I realized that the modulas “%” function is used to help realize that a math problem will result in a “0” answer, only if the prenumber is evenly divisible by the postnumber…

So all in all, I never got the correct code written to solve the problem. But, I did write this code that solves it a different way:

year = int(input())
If year%4 == 0 and year%100 == 0 and year%400 == 0:
print (“Leap year”)
Else:
print (“Not a leap year”)

So I just moved on in my lessons.

Thanks Rick,

Ya, thats what my brain finally realized…lol

I’m a commercial truck driver attempting to learn how to program. I have an algorithm in my mind, but haven’t found anyone to create it, so I decided to learn how to code - so that I can create the algorithm myself…

Appreciate the help!