7.11. Exercises¶
-
What do these expressions evaluate to?
3 == 3
3 != 3
3 >= 4
not (3 < 4)
(ex_6_1)
True
False
False
False
-
Give the logical opposites of these conditions. You are not allowed to use the
not
operator.a > b
a >= b
a >= 18 and day == 3
a >= 18 or day != 3
(ex_6_2)
-
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)
181def grade(mark):
2if mark >= 90:
3return "A"
4else:
5if mark >= 80:
6return "B"
7else:
8if mark >= 70:
9return "C"
10else:
11if mark >= 60:
12return "D"
13else:
14return "F"
15
16mark = 83
17print( "Mark:", str(mark), "Grade:", grade(mark))
18
(q3_question)
-
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.
(ex_6_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. 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).
(ex_6_5)
461import turtle
2
3def drawBar(t, height):
4""" Get turtle t to draw one bar, of height. """
5t.begin_fill() # start filling this shape
6if height < 0:
7t.write(str(height))
8t.left(90)
9t.forward(height)
10if height >= 0:
11t.write(str(height))
12t.right(90)
13t.forward(40)
14t.right(90)
15t.forward(height)
16t.left(90)
17t.end_fill() # stop filling this shape
18
19
20
21xs = [48, -50, 200, 240, 160, 260, 220] # here is the data
22maxheight = max(xs)
23minheight = min(xs)
24numbars = len(xs)
25border = 10
26
27tess = turtle.Turtle() # create tess and set some attributes
28tess.color("blue")
29tess.fillcolor("red")
30tess.pensize(3)
31
32wn = turtle.Screen() # Set up the window and its attributes
33wn.bgcolor("lightgreen")
34if minheight > 0:
35lly = 0
36else:
37lly = minheight - border
38
39wn.setworldcoordinates(0-border, lly, 40*numbars+border, maxheight+border)
40
41
42for a in xs:
43drawBar(tess, a)
44
45wn.exitonclick()
46
(answer_ex_6_5)
-
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 usesqrt
from the math module)(ex_6_6)
-
Write a function called
is_even(n)
that takes an integer as an argument and returnsTrue
if the argument is an even number andFalse
if it is odd.(ex_6_7)
131from test import testEqual
2
3def is_even(n):
4if n % 2 == 0:
5return True
6else:
7return False
8
9testEqual(is_even(10), True)
10testEqual(is_even(5), False)
11testEqual(is_even(1), False)
12testEqual(is_even(0), True)
13
(q7_answer)
-
Now write the function
is_odd(n)
that returnsTrue
whenn
is odd andFalse
otherwise.(ex_6_8)
-
Modify
is_odd
so that it uses a call tois_even
to determine if its argument is an odd integer.(ex_6_9)
191from test import testEqual
2
3def is_even(n):
4if n % 2 == 0:
5return True
6else:
7return False
8
9def is_odd(n):
10if is_even(n):
11return False
12else:
13return True
14
15testEqual(is_odd(10), False)
16testEqual(is_odd(5), True)
17testEqual(is_odd(1), True)
18testEqual(is_odd(0), False)
19
(q9_answer)
-
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 returnTrue
if the triangle is right-angled, orFalse
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 toy
, they would probably code it up asif abs(x - y) < 0.001: # if x is approximately equal to y ...
(ex_6_10)
-
Extend the above program so that the sides can be given to the function in any order.
(ex_6_11)
201from test import testEqual
2
3def is_rightangled(a, b, c):
4is_rightangled = False
5
6if a > b and a > c:
7is_rightangled = abs(b**2 + c**2 - a**2) < 0.001
8elif b > a and b > c:
9is_rightangled = abs(a**2 + c**2 - b**2) < 0.001
10else:
11is_rightangled = abs(a**2 + b**2 - c**2) < 0.001
12return is_rightangled
13
14testEqual(is_rightangled(1.5, 2.0, 2.5), True)
15testEqual(is_rightangled(4.0, 8.0, 16.0), False)
16testEqual(is_rightangled(4.1, 8.2, 9.1678787077), True)
17testEqual(is_rightangled(4.1, 8.2, 9.16787), True)
18testEqual(is_rightangled(4.1, 8.2, 9.168), False)
19testEqual(is_rightangled(0.5, 0.4, 0.64031), True)
20
(q11_answer)
-
3 criteria must be taken into account to identify leap years:
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 function that takes a year as a parameter and returns
True
if the year is a leap year,False
otherwise.(ex_6_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:
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
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)
191year = int(input("Please enter a year"))
2if year >= 1900 and year <= 2099:
3a = year % 19
4b = year % 4
5c = year % 7
6d = (19*a + 24) % 30
7e = (2*b + 4*c + 6*d + 5) % 7
8dateofeaster = 22 + d + e
9
10if year == 1954 or year == 2981 or year == 2049 or year == 2076:
11dateofeaster = dateofeaster - 7
12
13if dateofeaster > 31:
14print("April", dateofeaster - 31)
15else:
16print("March", dateofeaster)
17else:
18print("ERROR...year out of range")
19
(answer_ex_6_13)