Review: Exercise 9

Modify is_odd so that it uses a call to is_even to determine if its argument is an odd integer.

def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

def is_odd(number):
  if is_even(number):
        return False
    else:
        return True
      
print(is_odd(9))
print(is_odd(4))
print(is_odd(2))
print(is_odd(0))

Exercise 12

3 criteria must be taken into account to identify leap years:

  1. The year is evenly divisible by 4
  2. If the year can be evenly divided by 100, it is NOT a leap year, unless;
  3. The year is also evenly divisible by 400. Then it is a leap year.

Write a function that takes a year as a parameter and returns True if the year is a leap year, False otherwise.

#leap.py

def leap_year(year):
  if year % 100 == 0:
    if year % 400 == 0:
      return True
    else:
      return False
  elif year % 4 == 0:
    return True
  else:
    return False

print(leap_year(1900)) #False
print(leap_year(1996)) #True
print(leap_year(2000)) #True
print(leap_year(2016)) #True

Exercise 13

Implement the calculator for the date of Easter.

The following algorithm computes the date for Easter Sunday for any year between 1900 to 2099.

Ask the user to enter a year. Compute the following:

  1. a = year % 19
  2. b = year % 4
  3. c = year % 7
  4. d = (19 * a + 24) % 30
  5. e = (2 * b + 4 * c + 6 * d + 5) % 7
  6. dateofeaster = 22 + d + e

Special note: The algorithm can give a date in April. Also, if the year is one of four special years (1954, 1981, 2049, or 2076) then subtract 7 from the date.

Your program should print an error message if the user provides a date that is out of range.

year = int(input("Please enter a year"))

if year >= 1900 and year <= 2099:
    a = year % 19
    b = year % 4
    c = year % 7
    d = (19*a + 24) % 30
    e = (2*b + 4*c + 6*d + 5) % 7
   	easter_date = 22 + d + e

    if year == 1954 or year == 2981 or year == 2049 or year == 2076:
        easter_date = easter_date - 7

    if easter_date > 31:
        print("April", easter_date - 31)
    else:
        print("March", easter_date)
else:
    print("ERROR...year out of range")

Homework

  1. Read HTCS, 7.5-7.10
  2. Finish Project 5