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. Give the logical opposites of these conditions. You are not allowed to use the not operator.

    1. a > b
    2. a >= b
    3. a >= 18  and  day == 3
    4. a >= 18  or  day != 3
  3. Write a function which is given an exam mark, and it returns a string — the grade for that mark — according to this scheme:

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

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

    Test your function by printing the mark and the grade for a number of different marks.


    
    
    

    (ex_6_3)


    
    
    

    (q3_question)

  4. Write a function findHypot. The function will be given the length of two sides of a right-angled triangle and it should return the length of the hypotenuse. (Hint: x ** 0.5 will return the square root, or use sqrt from the math module)


    
    
    

    (ex_6_6)

  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.


    
    
    

    (ex_6_7)


    
    
    

    (q7_answer)

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


    
    
    

    (ex_6_8)

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


    
    
    

    (ex_6_9)


    
    
    

    (q9_answer)

  8. 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 as

    if  abs(x - y) < 0.001:      # if x is approximately equal to y
        ...
    

    
    
    

    (ex_6_10)

  9. Extend the above program so that the sides can be given to the function in any order.


    
    
    

    (ex_6_11)


    
    
    

    (q11_answer)

  10. Write the function is_prime(n) that returns True when n is prime and False otherwise.


    
    
    

    (ex_6_11b)

  11. A year is a leap year if it is divisible by 4 unless it is a century that is not divisible by 400. Write a function that takes a year as a parameter and returns True if the year is a leap year, False otherwise.


    
    
    

    (ex_6_12)

  12. 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.


    
    
    

    (ex_6_13)


    
    
    

    (answer_ex_6_13)

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.




(scratch_02)

Next Section - Iteration Revisited