Developing Procedures Homework
Understanding Developing Procedures in Python
Practice Developing Procedures
In these examples, you’ll be practicing developing procedures based off the context of the name and the call.
Practice 1:
def add(a, b): # what would you add here to make the function make sense later on?
return a + b
def show_sum(a, b):
total = add(a, b)
print("The sum is:", total)
show_sum(4, 7)
The sum is: 11
Practice 2:
def wake_up():
print("Wake up!")
def breakfast():
print("Eat breakfast!")
def get_ready():
print("Get ready for the day!")
# how would you change it so instead of having three seperate functions, you could only have one?
def morning_routine():
wake_up()
breakfast()
get_ready()
morning_routine()
Wake up!
Eat breakfast!
Get ready for the day!
Practice 3:
def greet(name): # how would you define the function to print "Hi (name)! and so that it can change on a name to name basis?"
print("Hi", name + "!")
def bye(name):
print("Bye", name + "!")
# Test different inputs
greet("Aneesh")
bye("Aneesh")
Hi Aneesh!
Bye Aneesh!
Combining Calling and Developing Procedures
Now that you understand both calling and developing, let’s combine the two to fully use functions! Your final task is to be able to produce, on your own, three procedures and call them in an order that makes sense. For example:
def get_grades():
grades = [87, 93, 88, 91, 90]
return grades
def grade_calc(grades):
total = sum(grades)
average = total / len(grades)
return average
def display_grade():
grades = get_grades()
average = grade_calc(grades)
print ("Your final average grade is:", average)
display_grade()
Your final average grade is: 89.8
The requirements for this are:
- must have three seperate procedures.
- must call all three procedures at least once, and it can be within other procedures.
- must have at least one print and at least one math function.
- at least one procedure must have multiple steps.
Other than that, have fun and create something as simple or complex as you’d like!
# put code here.
def wake_up():
print("Wake up!")
minutes_to_bed = 5
print(f"It took {minutes_to_bed} minutes to get out of bed.")
def breakfast():
print("Eat breakfast!")
calories = 300
print(f"You consumed {calories} calories for breakfast.")
def get_ready():
print("Get ready for the day!")
print("Brushing teeth...")
print("Getting dressed...")
print("Grabbing keys...")
steps = 20
print(f"It takes {steps} steps to get ready in the morning.")
def morning_routine():
wake_up()
breakfast()
get_ready()
morning_routine()
Wake up!
It took 5 minutes to get out of bed.
Eat breakfast!
You consumed 300 calories for breakfast.
Get ready for the day!
Brushing teeth...
Getting dressed...
Grabbing keys...
It takes 20 steps to get ready in the morning.