# Help with an assignment

Hey all! Me again!

I’ve got a current lab that I’m working on for my university course (intro to programming). My code is nearly complete, but bugs out on one input. The assignment is to determine whether or not the input year is a leap year or not. In order to be a leap year, the year must be divisible by 4 and if the year is a century year (1700, 1800, 1900, 2000) it must be divisible by 400. Here’s the code:

``````is_leap_year = False

input_year = int(input())

if (input_year % 4 == 0) and (input_year % 400 == 0):
print(input_year, "- leap year")
elif (input_year % 4 == 0):
print(input_year, "- leap year")
else:
print(input_year, "- not a leap year")
``````

A couple of things, quickly:

1. Not looking for the answer as this is an assignment. Alternatively if somebody could just point me in the right direction that would be great.
2. `is_leap_year = False` was prewritten code. I’m honestly not entirely sure how to use it in this instance.
3. The issue that I’m getting is that when the year ‘1900’ is input, it prints “1900 - leap year” because of the `input_year % 4 == 0` line, but if I remove that line, it won’t consider years like 2016 as leap years because they don’t meet the first conditional requirement.

For issue 3, that means you need another check before that point. Something that will handle the century year cases.

Issue 2 may depend on what your program is supposed to do. Is it supposed to print out the answer, or is it supposed to return to a caller? If you’re supposed to return, that may just be a default.

Thanks for the reply. I’ll try and find a way to write that check in.

the program is supposed to output `"input_year - leap year"` or `"input_year - not a leap year" `

Hey again! So my code now works, but I’m wondering if it’s the most efficient code. I’m going to post the code that I’ve submitted below. If you can think of a way that I could have written this more efficiently, can you let me know so that I can learn for next time? Thanks!

``````is_leap_year = False

input_year = int(input())

if (input_year % 400 == 0):
if (input_year % 4 == 0):
print(input_year, "- leap year")
else:
print(input_year, "- not a leap year")
else:
if (input_year % 100 != 0) and (input_year % 4 == 0):
print(input_year, "- leap year")
else:
print(input_year, "- not a leap year")
``````

This part seems odd to me:

``````if (input_year % 400 == 0):
if (input_year % 4 == 0):
``````

In what situation could that second if statement ever be false? What purpose does it have?

1 Like

It wouldn’t ever be false, good point. I probably should have reversed the statements. The purpose was to determine if a year had a 0 remainder for both dividing by 400 and by 4. This probably should have been written like `if (input_year % 4 == 0) and (input_year % 400 == 0):` instead.

400 is divisible by 4, so it doesn’t have to be checked separately.

`if (input_year % 4 == 0) and (input_year % 400 == 0):` is identical to
`if input_year % 400 == 0:`. The check for divisibility by 4 is superfluous.

1 Like

The purpose of `is_leap_year` is so you can avoid repeating the print calls multiple times. Instead set the variable appropriately in your if/else blocks, then afterwards check the variable to decide what to print. This way your code afterwards would also be able to use the is-leap functionality, and you haven’t repeated yourself.

1 Like

The variable was initialized to `False` as a default value. With that having been established, you just need to determine under what conditions we have a leap year. If those conditions exist, set the variable to `True`. Otherwise, leave the variable as is.

Thanks so much, Glenn. If it wouldn’t take too much time, would you mind showing me an example of how I could use the boolean operator to reduce the number of print lines?

See the two examples below for comparison.

Three `print` statements:

``````# without boolean variable
ticket_number = int(input("What number is on your ticket?: "))

# determine prize status and output result
if ticket_number % 700 == 0:
print("Ding! You won a shelf of Python books!")
elif ticket_number % 18 == 0 and ticket_number % 12 != 0:
print("Ding! You won a shelf of Python books!")
else:
print("Bzzz! You won a free 5 minute Java course!")
``````

Two `print` statements:

``````# with boolean variable
ticket_number = int(input("What number is on your ticket?: "))
won_good_prize = False

# determine prize status
if ticket_number % 700 == 0:
won_good_prize = True
elif ticket_number % 18 == 0 and ticket_number % 12 != 0:
won_good_prize = True

# output result
if won_good_prize:
print("Ding! You won a shelf of Python books!")
else:
print("Bzzz! You won a free 5 minute Java course!")

``````

Thanks so much! This was super helpful!

1 Like

Ok, so, probably not best for such an assignement, but if you ever wonder what they mean by python being batteries included:

``````import calendar
calendar.isleap(input_year)
``````

And if you want to have a look at how that function is implemented, you can always find the source code, which is also a great way to learn.