Credit: Written by Chris, Bryce, Brahm, and Lisa
Example: Average Function
File: mad_max.py
Complete the program by filling in the mad_max
function that returns the largest of three numbers:
def mad_max(x, y, z):
... your code here ...
The main
function has already been written for you:
def main():
print("The max of 4, 5, and 6 is:", mad_max(4, 5, 6))
print("The max of -4, 4, and 0 is:", mad_max(-4, 4, 0))
print("The max of 3, 2, and 1 is:", mad_max(3, 2, 1))
print("The max of 0, 0, and 0 is:", mad_max(0, 0, 0))
Your output should look like this:
The max of 4, 5, and 6 is: 6
The max of -4, 4, and 0 is: 4
The max of 3, 2, and 1 is: 3
The max of 0, 0, and 0 is: 0
While there is a built-in max
function in Python that we would normally use for this, for this problem you should not use it. Instead, we want you to get practice implementing it with other things, like if
, elif
and else
statements.
File: dog_years.py
Everyone knows that our furry friends age at a different rate to humans. Write a program that asks the user for a human age (expressed as a whole number) and prints the equivalent dog age using the fact that there are seven dog years per human year. Consider defining a constant DOG_YEARS_PER_HUMAN_YEAR
. You can assume the user types in an integer age, but not necessarily that the integer is positive. If it isn't, print an error message. If the user enters 0, the program should stop.
Your program should define a function called compute_dog_years
which accepts as a parameter an integer called human_years
. The function should return the converted human age. You should then edit the main
function to call this function.
def compute_dog_years(human_years):
... your code here ...
The main function is already written for you, but it is missing the code that calls your compute_dog_years
function and prints out the correct statement.
def main():
user_input = int(input("Enter an age in human years: "))
while user_input != 0:
if user_input < 0:
print("Sorry, please enter a positive number or 0 to exit")
else:
... your code here ...
user_input = int(input("Enter an age in human years: "))
Here is an example of what one run of your program should look like (user inputs -12, 13, and 0, in order):
Enter an age in human years: -12
Sorry, please enter a positive number or 0 to exit
Enter an age in human years: 13
The age in dog years is 91
Enter an age in human years: 0