Exercise 4.1

  1. Write expressions to compute the following.

    • x and y are not equal
    • x and y are both equal to 1
    • the value of age indicates a teenager
    • the value of year indicates a leap year
      Nb. leap years are divisible by 4 but not by 100, or divisible by 400.
  2. Given that a is assigned the value 3, what do the following expressions return, if anything?

    • True and False
    • b = (a != 3)
    • 0.3 == (0.1 + 0.2)

Exercise 4.2

Write a program that reads an integer from the user - 0, 1, 2 - and returns the corresponding name – “zero”, “one” or “other”.

If this feels a bit tricky, try implementing a solution for “one” and “other” first, and then add support for “two”.

Exercise 4.3

Write a program that inputs three integers from the user and prints the largest. Don’t use a list max to solve this problem.

If this feels a bit tricky, try implementing a solution for only two integers and then adding support for the third integer as time allows.

If this seems too easy, try printing the three integers in ascending order (without using list sort).

Exercise 4.4

Write a program that prints the appropriate plan cost based on values of months and plan, and the following table.

  months
plan 1 2-6 7+
silver $90 $60 $60
gold $90 $70 $35

For example, a 1-month (month == 1), silver plan (plan == 'silver') is $90.