Ask coding questions

← Back to all posts
Needing help with IF else statements
jhamilton85 (0)

I have an assignment where I have to create a python program to determine if a year is a leap year or not. I have the variable named year and value : year = 2018
My instructions say to put in an if else statement of:
if(year % 4)==0
if(year % 100)==0
print ("{0} is not a leap year" .format(year))

But no matter how I put this in, it says error....this is my first time using python and I am getting frustrated...Any help is appreciated !

Coder100 (18118)

don't forget to print it, like this:

year = 2018
if (year % 4) == 0:
  print("ya its a leap year")

the syntax is like this:

if <expression is true>:

don't forget the :!

jhamilton85 (0)

Thank you ! I didnt even see that. I have the code written but it doesn't act like it is working right. Could you see what I am doing wrong ?

Coder100 (18118)

don't forget to indent properly! @jhamilton85

Coder100 (18118)

here is the correct code:

# Program to check if is this a leap year or not
year = 2018
if (year % 4) == 0 and (year % 100) == 0 and (year % 400) == 0:
  print ("{0} is not a leap year".format(year))
  print("{0} is not a leap year".format(year))

I slightly changed it so you don't have to type as much. This new keyword is called the and operator. It will return true if both sides are true


jhamilton85 (0)

Thank you so much....After looking at what you did it is starting to make more sense

Coder100 (18118)

np! mark as correct answer please @jhamilton85