# Help with an assignment

**URL:** <https://discuss.python.org/t/help-with-an-assignment/11347>\
**Category:** Python Help\
**Created:** [October 19, 2021, 4:36pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347 "2021-10-19T16:36:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 19, 2021, 4:36pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/1 "2021-10-19T16:36:02Z")

</div>

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:

```auto
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.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 19, 2021, 4:59pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/2 "2021-10-19T16:59:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 19, 2021, 5:02pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/3 "2021-10-19T17:02:40Z")

</div>

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" `

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 19, 2021, 5:15pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/4 "2021-10-19T17:15:11Z")

</div>

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!

```auto
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") 

```

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 19, 2021, 5:28pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/5 "2021-10-19T17:28:28Z")

</div>

This part seems odd to me:

```python
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?

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 19, 2021, 7:34pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/6 "2021-10-19T19:34:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 19, 2021, 7:55pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/7 "2021-10-19T19:55:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TeamSpen210](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/teamspen210/32/1004_2.png) [@TeamSpen210](https://discuss.python.org/u/TeamSpen210)\
**Post date:** [October 19, 2021, 8:28pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/8 "2021-10-19T20:28:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [October 19, 2021, 9:47pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/9 "2021-10-19T21:47:39Z")

</div>

> [@Rohagan4](#):
>
> 1. `is_leap_year = False` was prewritten code. I’m honestly not entirely sure how to use it in this instance.

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.

> [@TeamSpen210](#):
>
> … then afterwards check the variable to decide what to print.

After you have followed that advice, the task is complete.

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 19, 2021, 10:17pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/10 "2021-10-19T22:17:58Z")

</div>

> [@Quercus](#):
>
> 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?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [October 19, 2021, 11:46pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/11 "2021-10-19T23:46:40Z")

</div>

See the two examples below for comparison.

Three `print` statements:

```auto
# 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:

```auto
# 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!")

```

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 20, 2021, 12:19am UTC](https://discuss.python.org/t/help-with-an-assignment/11347/12 "2021-10-20T00:19:48Z")

</div>

Thanks so much! This was super helpful!

---

<div class="post-metadata">

**Author:** ![dandiez](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dandiez/32/3185_2.png) [@dandiez](https://discuss.python.org/u/dandiez)\
**Post date:** [October 22, 2021, 6:45pm UTC](https://discuss.python.org/t/help-with-an-assignment/11347/13 "2021-10-22T18:45:24Z")

</div>

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

```auto
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.

> <https://github.com/python/cpython/blob/main/Lib/calendar.py>
