Python exercises: Functions

Note The exercises below are adapted from the Python for Everyone Course by Charles R. Severance licensed under Creative Commons Attribution 3.0

Exercise 1. Sequence of function calls

What will the following Python program print out?

def fred():
   print("Zap")

def jane():
   print("ABC")

jane()
fred()
jane()

a) Zap ABC jane fred jane

b) Zap ABC Zap

c) ABC Zap jane

d) ABC Zap ABC

e) Zap Zap Zap

Check your answer by running the code

# your code here

Exercise 2. Pay computation with a function

Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay which takes two parameters (hours and rate).

Enter Hours: 45
Enter Rate: 10
Pay: 475.0
# your code here

Exercise 3. Grade calculation using a function

Rewrite the grade program from the 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.

# your code here

Exercise 4. Check age of your participant

Rewrite the check age program from the previous chapter using a function that takes the age and the name and returns the message as a string.

# your code here