10. Exercises

  1. What do these expressions evaluate to?

    1. 3 == 3

    2. 3 != 3

    3. 3 >= 4

    4. not (3 < 4)

    1. True
    2. False
    3. False
    4. False
  2. Write a function which is given an exam score, and it returns the corresponding letter grade as a string according to this scheme:

    score Grade
    >= 90 A
    [80-90) B
    [70-80) C
    [60-70) D
    < 60 F

    The square and round brackets denote closed and open intervals, respectively. A closed interval includes the number, an open interval excludes it. So 79.99999 gets grade C , but 80 gets grade B.

    Test your function by printing the score and the grade for a number of different scores.

  3. Modify the turtle bar chart program from the previous chapter so that the bar for any value of 200 or more is filled with red, values between [100 and 200) are filled yellow, and bars representing values less than 100 are filled green.

  4. In the Turtle bar chart program, what do you expect to happen if one or more of the data values in the list is negative? Go back and try it out. Then change the program so that when it prints the text value for the negative bars, it puts the text above the base of the bar (on the 0 axis).

  5. Write a function called is_even(n) that takes an integer as an argument and returns True if the argument is an even number and False if it is odd. Note that instead of printing out the results we are using test statements. The goal is to pass all the tests that are listed underneath the function you will write. You do not need to add a main function to this code to run it.

  6. Now write the function is_odd(n) that returns True when n is odd and False otherwise.

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

  8. Write a fruitful function called pick_activity to help you pick an activity to engage in based on the current weather. It has two parameters, one for how hot it is and one for how wet it is. If it is hot and wet, it should tell you to watch Netflix. If it hot and dry, it should tell you to go swimming. If it is cold and wet, it should tell you to paint. If it is cold and dry, it should tell you to go to a cafe and read. Use the elif construct.

  9. Write a function is_rightangled which, given the length of three sides of a triangle, will determine whether the triangle is right-angled. Assume that the third argument to the function is always the longest side. It will return True if the triangle is right-angled, or False otherwise.

    Hint: floating point arithmetic is not always exactly accurate, so it is not safe to test floating point numbers for equality. If a good programmer wants to know whether x is equal or close enough to y, they would probably code it up using the abs() function like so:

    if  abs(x - y) < 0.001:      # if x is approximately equal to y
        ...
    
  10. Extend the above program so that the sides can be given to the function in any order.

  11. Implement the calculator for the date of Easter.

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

    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. date = 22 + d + e

    Special note: The algorithm can give a date greater than 31 (the number of days in March). When this happens, it signifies a date in April. Thus, 32 is April 1, 35 is April 4, and so on. 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.

Graded Lesson Assignment

A year is a leap year if it is divisible by 4, unless it is the first year of a century and it is not divisible by 400.

For example:

  • 1956 is a leap year because 1956 is divisible by 4.
  • 1957 is not a leap year, because it is not divisible by 4.
  • 1900 is not a leap year, despite the fact that it is divisible by 4, because 1900 is the first year of a century and 1900 is not divisible by 400.
  • 1600 is a leap year, because 1600 is divisible by 4 and 1600 (although it is the first year of a century) is divisible by 400

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

Ready to submit your code? Time to fork, and submit a Pull Request to: this Github Repository.