Welcome back to my blog!
For those of you that have read last month's post about autism, I hope you enjoyed it and learned a bit more about autistic people 👼 If you haven't read it yet, here is the link to the article.
For the last few months, I have been focused on perfecting the Guess the Number game but I felt like after three consecutive posts, more projects related to Guess the Number would become repetitive if I continued any longer. This time, I've decided to make a code focused on letting you know whether the number you've entered is a leap year.
What is a leap year?
Simply put, a leap year is any year that has an extra day. For most years, February only has 28 days in the month. Every four years, February has 29 days instead! Coincidentally, the Olympics is almost always held on a leap year! (Unfortunate for 2020, but alas...)
In modern times, a year must fulfill the following criteria to be called a leap year:
- MUST be divisible by 4
- MUST NOT be divisible by 100
- EXCEPT IF a year is divisible by 400, then it WILL BE a leap year
How did you make a leap year checker?
Basically, I used loops to consider the above three criteria and check whether the user has entered a leap year or not. The techniques are pretty much the same as I used for the Guess the Number codes, so I'll leave the relevant posts that talk more about loops in depth.
Here are the links for the past Guess the Number projects:
Below is the Leap Year Checker code that I've built.
Leap year checker code: Please download the PDF from the link or by looking at the image posted here.
Let's look back at what makes a leap year:
- MUST be divisible by 4
- MUST NOT be divisible by 100
- EXCEPT IF a year is divisible by 400, then it WILL BE a leap year
There is a bit of math involved, but don't worry because it's super simple! You can see the symbol "%" in three lines. It means "divide a number by X, then figure out the remainder." In this scenario,
- "if year%4 == 0" means "if the remainder is 0 when year is divided by 4"
- "if year%100 == 0" means "if the remainder is 0 when year is divided by 100"
- "if year%400 == 0" means "if the remainder is 0 when year is divided by 400"
A remainder is a "leftover" number after a number was divided by another number. For example, if 3 was divided by 2, there would be a remainder of 1 because 3 = 2 x 1+1. If the remainder is 0, that means that the number is divisible by the other number. For example, if 2 was divided by 2, there would be a remainder of 0 because 2 = 2 x 1 + 0. Therefore when re-wording the code lines,
- "if year%4 == 0" means "if the year is divisible by 4"
- "if year%100 == 0" means "if the year is divisible by 100"
- "if year%400 == 0" means "if the year is divisible by 400"
I hope that I've explained the concepts well so that you can understand the logic behind the code 😉
What happens when you use the code?
Here are a couple of playthroughs that demonstrates what the output would look like.
Playthrough 1: Please download the PDF from the link or by looking at the image here.
Playthrough 2: Please download the PDF from the link or by looking at the image here.
The code works by generating a message depending on whatever the user inputs in the "Enter year:" line. If the user enters a year that's not a leap year, it will return a "NOT a leap year" message. If the user enters a year that is a leap year, it will return a "Leap year!" message. If the user enters a word that isn't a number, it will return a "Error, please check input" message. The user can continue to enter numbers for as long as they like until they enter a leap year, then it will be the "End."
Final messages
I hope you've enjoyed this month's post as well! I've made a few changes in my writing style since my last project-related post. The code itself was pretty easy to make, and if you've made your own versions of leap year checker codes, then I'd be pretty excited to see them 😃 As always, please feel free to leave a comment or send me a message. Contact information can be found on the
Contact Me page.
I haven't figured out what project I'll be doing next month 😂 But rest assured I still plan to keep posting for the foreseeable future 💗 Please check out the archives section, for my past posts until next month!
No comments:
Post a Comment