-1

I am writing some code in order to test if a year is a leap year or not.

So I wrote:

year = input("please enter a year: ")

if (year % 4) == 0:

    print(f"{year} is a leap year.")

else:

    print(f"{year} is a nonleap year.")

And the error reported is:

    if (year % 4) == 0:
TypeError: not all arguments converted during string formatting

2 Answers2

0

As year is a string, year % 4 tries to run a string formatting operation. Change to

if (int(year) % 4) == 0:

clubby789
  • 2,543
  • 4
  • 16
  • 32
0

to check if it's the leap year you can use

import calendar
print calendar.isleap(2019)

if you want to keep your way, you need to convert year into int:

int(year)
krolikmvp
  • 11
  • 2
  • 1
    Hardcoding `2019` in the code doesn't help at all, making the `calendar` use effectively a non-sequiteur -- if they tried to keep their existing `year = input("please enter a year: ")` and use `print(calendar.isleap(year))` with the Python version they're already using, they'd have a very similar problem to the existing one. – Charles Duffy Aug 03 '19 at 00:20
  • Right, I didn't mean to hardcode the date, just wanted to show the usage of calendar – krolikmvp Aug 03 '19 at 07:44