Skip to main content

Exercises 4.11 Exercises

1.

What do these expressions evaluate to?
  1. 3 == 3
  2. 3 != 3
  3. 3 >= 4
  4. not (3 < 4)
Solution.
  1. True
  2. False
  3. False
  4. False

2.

    Q-9: Given two variables, num1 and num2, which of the following would mean that both num1 and num2 are positive integers?
  • (num1 == num2)
  • Incorrect. The two variables can be equal to each other and still be negative integers.
  • (num1 == num2) or (num1 > 0)
  • Incorrect. This is true if the two numbers are equal or num1 is greater than 0.
  • (num1 == num2) and (num1 > 0)
  • If num1 is equal to num2 and num1 is greater than 0, then both values must be positive.
  • (num1 == num2) and (num1 < 0)
  • Incorrect. The two variables would both be negative in this case.

3.

    Q-12: Which of the following evaluates to True when a is equal to b or when a is equal to 5?
  • a == b == 5
  • You can not join conditionals in this way.
  • a = b or a = 5
  • You must use == to test for equality.
  • a == b or a == 5
  • This will be true if a is equal to b or a is equal to 5.
  • a = b and a = 5
  • You must use == to test for equality.
  • a == b and a = 5
  • This will only be true when a and b both equal 5.

4.

    Q-6: Which of the following is true about the code below?
    x = 3
    if (x > 2):
        x = x * 2;
    if (x > 4):
        x = 0;
    print(x)
    
  • x will always equal 0 after this code executes for any value of x
  • If x was set to 1 originally, then it would still equal 1.
  • if x is greater than 2, the value in x will be doubled after this code executes
  • What happens in the original when x is greater than 2? Note: these are separate if statements.
  • if x is greater than 2, x will equal 0 after this code executes
  • If x is greater than 2, it will be set to 0.

5.

    Q-1: Which of following uses the correct syntax for the header of a Python if statement?
  • if (x = y):
  • Try again! An if statement shouldn’t contain an assignment operator.
  • if (x == y):
  • Correct! The parentheses are not necessary, but they don’t hurt either.
  • if !(x is not y):
  • Try again! The not operator (!) can’t be used in python.
  • if x = y:
  • Try again! An if statement shouldn’t contain an assignment operator.
  • if x == y:
  • Correct!

6.

    Q-2: Which of the following tests if 5 exponentiated to the 4th power is greater than y and less than or equal to z?
  • z >= 5**4 > y
  • Correct! Note that this form of compound condition is allowed in Python. Many other languages will not allow this!
  • 5**4 > y and (5**4 < z or 5**4 == z)
  • Correct! Note that the and operator comes before the or operator in operator precedence.
  • 5**4 > y and <= z
  • Try again! Nothing is being compared to z.
  • 5**4 > y and 5**4 < z or 5**4 == z
  • Try again! The and operator comes before the or operator in operator precedence.
  • 5**4 > y and z >= 5**4
  • Correct! Note that z >= 5**4 is the same as 5**4 <= z.

7.

Give the logical opposites of these conditions. You are not allowed to use the not operator. (This exercise requires the material from Subsection 4.5.2)
  1. a > b
  2. a >= b
  3. a >= 18 and day == 3
  4. a >= 18 or day != 3
Solution.
  • a <= b
  • a < b
  • a < 18 or day != 3
  • a < 18 and day == 3

8.

Write code which, given an exam mark, sets a string to the grade for that mark according to this scheme:
Table 4.11.1.
Mark Grade
>= 90 A
[80-90) B
[70-80) C
[60-70) D
< 60 F
The square bracket and parenthesis 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 grade for a number of different scores.
Solution.
mark = float(input("Enter mark:"))
if mark >= 90:
    grade = "A"
elif mark >= 80:
    grade = "B"
elif mark >= 70:
    grade = "C"
elif mark >= 60:
    grade = "D"
else:
    grade = "F"
print("Your grade is", grade)

9.

Write a program which, given the length of three sides of a triangle, will determine whether the triangle is right-angled. The sides of the triangle will be called a, b, and c 1  where c is always the longest side. It will print Yes if the triangle is right-angled, or No 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
    ...
Try your code with these values for a, b, and c:
  • 1.5, 2.0, 2.5 (should be Yes)
  • 4.0, 8.0, 11.0 (should be No)
  • 4.1, 8.2, 9.16787 (should be Yes)

10.

How do we determine if a year is a leap year? We use these three criteria:
  • The year is evenly divisible by 4;
  • If the year can be evenly divided by 100, it is NOT a leap year, unless;
  • The year is also evenly divisible by 400. Then it is a leap year.
  • Write a program that takes a year as input and prints whether the year is a leap year or not.

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.
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_of_easter = 22 + d + e
The date_of_easter will give the day in March when Easter occurs. If this value is greater than 31, then the day is in April, and you must subtract 31 from the date_of_easter variable.
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.
Solution.
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
    date_of_easter = 22 + d + e

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

    if date_of_easter > 31:
        print("April", date_of_easter - 31)
    else:
        print("March", date_of_easter)
else:
    print("ERROR...year out of range")
We usually want longer names, but in geometry, it is common to label the sides of a triangle as a, b, and c, so we will use that convention.