Python for Everybody
Chapter 4
Exercise 4.1
"""
Exercise 4.1: Run the program on your system and see what numbers you get.
Run the program more than once and see what numbers you get.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 28, 2017
"""
import random
for i in range(10):
x = random.random()
print(x)
"""
Run 1:
0.21764319507444463
0.8329991443974214
0.5701549669913151
0.8637443412384684
0.2047094119147722
0.3202386315168375
0.9782522613350779
0.9220494004895224
0.17966209998031546
0.7983521239029091
Run 2:
0.47116913276761185
0.49784628316541546
0.7292518476972524
0.2355420735182987
0.16876377822830468
0.6365600615263461
0.1689585397335408
0.41161696529382463
0.9895980083921391
0.23023688059069947
"""
Exercise 4.2
"""
Exercise 4.2: Move the last line of this program to the top, so the function
call appears before the definitions. Run the program and see what error
message you get.
Code: http://www.py4e.com/code3/lyrics.py
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 28, 2017
"""
repeat_lyrics()
def repeat_lyrics():
print_lyrics()
print_lyrics()
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
"""
Error message:
Traceback (most recent call last):
File "/home/jamison/workspace/python-for-everybody/exercise4_2.py", line 14, in <module>
repeat_lyrics()
NameError: name 'repeat_lyrics' is not defined
"""
Exercise 4.6
"""
Exercise 4.6: Rewrite your pay computation with time-and-a-half for overtime
and create a function called computepay which takes two paramteters (hours and
rate).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 28, 2017
"""
def computepay(tmp_hours, tmp_rate):
"""
Calculates the amount to pay taking into account overtime
Inputs: tmp_hours -- the total hours worked
tmp_rate -- pay rate of the employee
Output: amount due to employee
"""
if tmp_hours <= 40.0:
return tmp_rate * tmp_hours # No overtime calculation
# Since the value is returned if hours <= 40, we no longer need the
# else statement here.
overtime = tmp_hours - 40.0 # How much overtime is left
return (tmp_rate * 40.0) + (1.5 * tmp_rate * overtime)
def check_for_float(input1):
"""
Checks if the type of "input1" is a float and returns the value if so.
Input: input1 -- variable to check
Output: val -- value of float
"""
try:
val = float(input1) # Only allows input floats
return val
except ValueError:
print('Error, please enter numeric input')
quit()
input_hours = input('Enter Hours: ')
hours = check_for_float(input_hours)
input_rate = input('Enter Rate: ')
rate = check_for_float(input_rate)
pay = computepay(hours, rate)
print('Pay: ', pay)
Exercise 4.7
"""
Exercise 4.7: Rewrite the grade program from previous chapter using a function
called computegrade that takes a score as its parameter and returns a grade as
a string.
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
~~~
Enter score: 0.95
A
Enter score: perfect
Bad score
Enter score: 10.0
Bad score
Enter score: 0.75
C
Enter score: 0.5
F
Run the program repeatedly to test the various, different values for input.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 28, 2017
"""
def computegrade(tmp_score):
"""
Computes the final grade based on a 0.0 to 1.0 scale. If the score is
not between 0.0 and 1.0, returns "Bad score."
Input: score -- score (must be between 0.0 and 1.0)
Output: returns a grade as a string.
"""
if 0.0 <= tmp_score <= 1.0:
if tmp_score >= 0.9:
return 'A'
if tmp_score >= 0.8:
return 'B'
if tmp_score >= 0.7:
return 'C'
if tmp_score >= 0.6:
return 'D'
return 'F'
# Case 'score' is not on the interval
return 'Bad score'
input_score = input('Enter score: ')
score = 0.0
try:
score = float(input_score) # Only allows input floats
except ValueError:
print('Bad score')
quit()
grade = computegrade(score)
print(grade)