During this activity, students should be able to:
if
statements).
Solve the following set of problems using Python 3. Run and test each of your programs to make sure they work as expected.
All the following problems must be solved using if
conditionals.
Write a program called grade.py
. Define in this program a function called grade(score)
that returns a string with the corresponding letter
grade for a given numerical score. Use the values from the following table:
Score  Grade 

From 95 to 100 
'A'

From 85 to 94 
'B'

From 70 to 84 
'C'

From 50 to 69 
'D'

From 0 to 49 
'F'

Any value less than 0 or greater than 100 
'ERROR'

The program’s main()
function should prompt the user for a score and print the corresponding grade.
Example (user input is in blue):
Give me a score between 0 and 100: 76
The grade is: C
Write a program called max3.py
. Define in this program a function called max3(x, y, z)
that returns the largest of x
, y
, and z
. Do
not use the builtin Python function max()
. The program’s main()
function should prompt three values from the user and print the one that is largest.
Example (user input is in blue):
First value: 34 Second value: 25 Third value: 16 The largest value is: 25
Write a program called sort3.py
. Define in this program a function called sort3(x, y, z)
that returns the three values x
, y
, and z
in a sorted order tuple (a, b, c)
, where a
≤ b
≤ c
. Do not use any builtin
Python sorting functions, but you may put if
statements inside of other
if
statements. The program’s main()
function should prompt three values from the user and print them sorted.
Example 1 (user input is in blue):
First value: 34 Second value: 16 Third value: 25 Sorted values: (34, 16, 25)
Example 2:
First value: 34 Second value: 25 Third value: 16 Sorted values: (34, 16, 25)
Example 3:
First value: 16 Second value: 34 Third value: 25 Sorted values: (34, 16, 25)
Example 4:
First value: 16 Second value: 25 Third value: 34 Sorted values: (34, 16, 25)
Example 5:
First value: 25 Second value: 34 Third value: 16 Sorted values: (34, 16, 25)
Example 6:
First value: 25 Second value: 16 Third value: 34 Sorted values: (34, 16, 25)
Given a quadratic equation \(ax^2+bx+c\), we define its discriminant as:
$$ b^2  4ac $$If the discriminant is a positive number greater than zero, the quadratic equation has two real roots. If the discriminant is equal to zero, the quadratic equation has one real root. Finally, if the discriminant is a negative number, the quadratic equation has no real roots.
Write a program called roots.py
. Define in this program a function called numroots(a, b, c)
that returns the number of real roots (0, 1, or 2) for a quadratic equation given its coefficients a
, b
, and c
. Use the following main()
function to test your numroots()
function:
def main(): print(numroots(4, 5, 1)) print(numroots(2, 4, 2)) print(numroots(1, 2, 3))
The program’s output should look like this:
2 1 0
Write a program called generation.py
. Define in this program a function called generation(y)
that returns a string indicating the generation to which a person belongs given the year y
in which she was born. Use the information in this table:
Year Born  Generation 

1945 or before 
'Mature'

Between 1946 and 1964 
'Baby Boomer'

Between 1965 and 1980 
'Generation X'

Between 1981 and 2000 
'Millennium'

2001 or after 
'Boomlet'

Use the following main()
function to test your generation()
function:
def main(): for i in range(1945, 2006, 5): print(i, generation(i))
The program’s output should look like this:
1945 Mature 1950 Baby Boomer 1955 Baby Boomer 1960 Baby Boomer 1965 Generation X 1970 Generation X 1975 Generation X 1980 Generation X 1985 Millennium 1990 Millennium 1995 Millennium 2000 Millennium 2005 Boomlet
Write a program called triangles.py
. Define in this program a function called triangle_kind(x1, y1, x2, y2, x3, y3)
. The six input parameters correspond to three points on a twodimensional coordinate plane: \((x_1, y_1), (x_2, y_2)\) and \((x_3, y_3)\). Assume the three points are connected in order to form a triangle. The function should return a string indicating what kind of triangle they formed: 'right'
, 'equilateral'
, 'isosceles'
, or 'scalene'
.
Have in mind that:
The distance \(d\) between two points \((x_1, y_1)\) and \((x_2, y_2)\) can be calculated using this formula:
$$ d=\sqrt{(x_2x_1)^2 + (y_2  y_1)^2} $$A triangle is right if Pythagoras’ theorem holds true. That is, for a triangle with sides of length \(a, b,\) and \(c\) (where \(a < c\) and \(b < c\)):
$$ a^2 + b^2 = c^2 $$A triangle is equilateral if all of its sides are equal.
A triangle is isosceles if only two of its sides are equal.
A triangle is scalene if all of its sides are different.
VERY IMPORTANT: Given the imprecisions that might occur when comparing two floating point numbers p
and q
, instead of doing:
p == q
you should use the isclose()
from the Python 3 math
module, like this:
isclose(p, q)
And, instead of doing:
p != q
do this:
not isclose(p, q)
Don’t forget to import the isclose()
function when using it in your programs:
from math import isclose
Use the following main()
function to test your triangle_kind()
function:
from math import sqrt def main(): print(triangle_kind(1, 2, 2, 3, 4, 2)) print(triangle_kind(1, 1, 2, 3, 3, 1)) print(triangle_kind(1, 3, 1, 5, 1 + sqrt(3), 4)) print(triangle_kind(5, 5, 3, 5, 3, 1))
The program’s output should be:
scalene isosceles equilateral right
Some of the previous problems were taken from [JOHNSON] chapter 6 and from:
Kenneth Lambert.
Fundamentals of Python: From First Programs through Data Structures.
CENGAGE Learning, 2010.
p. 118.