Skip to main content

Section 6.13 Exercises

Checkpoint 6.13.1.

Write code that iterates through the given list "scores" and checks which letter grade each score has earned. In response, it should add the corresponding letter grade to the empty list "grades", according to the table below.
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. A closed interval includes the number, and open interval excludes it. So 79.99999 gets grade C , but 80 gets grade B.
Solution.
scores = [77.51, 92.86, 98.01, 69.71, 78.52, 59.69, 60.49, 85.04, 87.33, 91.04]
grades = []
for fl_sc in scores:
        if fl_sc < 60:
        gr = "F"
        grades.append(gr)
    elif fl_sc < 70:
        gr = "D"
        grades.append(gr)
    elif fl_sc < 80:
        gr = "C"
        grades.append(gr)
    elif fl_sc < 90:
        gr = "B"
        grades.append(gr)
    else:
        gr = "A"
        grades.append(gr)

Checkpoint 6.13.2.

A year is a leap year if it is divisible by 4; however, 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 code that iterates through the given list of years and adds leap years to a list of booleans called "is_leap_year". Use if statements.
Year Leap?
1944 True
2011 False
1986 False
1800 False
1900 False
2000 True
2056 True
Above are some examples of what the output should be for various inputs.
Solution.
years = [1967, 1900, 1400, 1628, 1701, 1217, 1359, 1300, 2000, 1054,
1724, 1000, 1800, 1100, 2100, 1023, 1600, 1500, 1358, 1160,
1700, 1744, 2009, 1200]

is_leap_year = []           
for year in years:
    if int(year) % 4 == 0:
        if int(year) % 100 == 0:
            if int(year) % 400 == 0:
                is_leap_year.append(True)
            else:
                is_leap_year.append(False)
        else:
            is_leap_year.append(True)
    else:
        is_leap_year.append(False)

Checkpoint 6.13.3.

    What do these expressions evaluate to?
    1. 3 == 3
    2. 3 != 3
    3. 3 >= 4
    4. not (3 < 4)
  • True, False, False, False.
  • This is correct!
  • True, True, False, False.
  • The "!=" operator is used to mean "does not equal".
  • True, False, True, False.
  • The ">=" operator means "lesser than or equal to".
  • True, False, False, True.
  • The "not" operator returns the opposite of whatever boolean value is stored inside.

Checkpoint 6.13.4.

Give the logical opposites of these conditions, meaning an expression that would produce False whenever this expression produces True, and vice versa. 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
Solution.
a <= b
a < b
a < 18 or day != 3
a < 18 and day == 3

Checkpoint 6.13.5.

Provided are the lengths of two sides of a right-angled triangle. Assign the length of the hypotenuse the the variable hypo_len. (Hint: x ** 0.5 will return the square root, or use sqrt from the math module)

Checkpoint 6.13.6.

Provided is a list of numbers. For each of the numbers in the list, determine whether they are even. If the number is even, add True to a new list called is_even. If the number is odd, then add False.

Checkpoint 6.13.7.

Provided is a list of numbers. For each of the numbers in the list, determine whether they are odd. If the number is odd, add True to a new list called is_odd. If the number is even, then add False.

Checkpoint 6.13.8.

Given the lengths of three sides of a triange, determine whether the triangle is right angled. If it is, the assign True to the variable is_rightangled. If it’s not, then assign False to the variable is_rightangled.
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
    ...

Checkpoint 6.13.9.

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. You will know that the date is in April if the calculation gives you an answer greater than 31. (You’ll need to adjust) 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.
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
    dateofeaster = 22 + d + e

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

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

Checkpoint 6.13.10.

Get the user to enter some text and print out True if it’s a palindrome, False otherwise. (Hint: Start by reversing the input string, and then use the == operator to compare two values to see if they are the same)

Checkpoint 6.13.11.

Write a program that will print out a greeting to each student in the list. This list should also keep track of how many students have been greeted and note that each time a new student has been greeted. When only one student has entered, the program should say β€œThe first student has entered!”. Afterwards, the program should say β€œThere are {number here} students in the classroom!”.

Checkpoint 6.13.12.

Piece together a program so that it can successfully print out one print statement, given the value of x.
You have attempted of activities on this page.