A function may need more than one value. List the parameters with commas between them, and pass the arguments in the same number and order. This function adds the times of three race stages, and we store the result for each team:
def add_times(stage1_time, stage2_time, stage3_time):
time = stage1_time + stage2_time + stage3_time
return time
teamA = add_times(37.5, 62.5, 56.5)
teamB = add_times(44, 57, 64)
print(teamA)
print(teamB)
print("The fastest time is " + str(min(teamA, teamB)))156.5 165 The fastest time is 156.5
TypeError.A function may need no value at all; write empty brackets both when you define it and when you call it:
def good_evening():
greeting = "Good evening!"
return greeting
print(good_evening())Good evening!
if and for inside a function
The body of a function is a small program, so it can contain conditions. return ends the function at once; nothing after it runs:
def grade(score):
if score >= 85:
return "Excellent"
elif score >= 50:
return "Pass"
else:
return "Try again"
for s in [92, 70, 40]:
print(grade(s))Excellent Pass Try again
It can also contain a loop. This function multiplies a number by itself a number of times:
def power(base, times):
num = 1
for i in range(times):
num = num * base
return num
print(power(3, 4))
print(power(2, 10))81 1024
From flowchart to code
Before writing a program with nested conditions, draw a flowchart to order the steps. Each shape has a match in Python:
| Shape | Meaning | In Python |
|---|---|---|
| Diamond | Yes / no question | if weather == "sunny": |
| "No" arrow | The other path | else: |
| Rectangle | An action | print("play soccer") |
| Diamond after "yes" | A condition inside a condition | an indented if inside an if |
weather = "sunny"
friends = 11
if weather == "sunny":
if friends >= 11:
print("play soccer")
else:
print("play badminton")
else:
print("watch a movie")play soccer
Practice
Add a third parameter stage3_time to the function and to the sum, and pass 56.5 when calling it.
def add_times(stage1_time, stage2_time, ___):
time = stage1_time + stage2_time + ___
return time
teamA = add_times(37.5, 62.5, ___)
print(teamA)Press Run to see the result. On the last line print The fastest time is followed by the smallest of teamA, teamB and teamC (use min and str).
def add_times(stage1_time, stage2_time, stage3_time):
time = stage1_time + stage2_time + stage3_time
return time
teamA = add_times(37.5, 62.5, 56.5)
teamB = add_times(44, 57, 64)
teamC = add_times(42, 63.5, 59)
Press Run to see the result. Create a function mul with parameters num1 and num2 that returns their product, then print mul(9, 7).
Press Run to see the result. Create a function good_morning with no arguments that returns the text Good morning, then call it and print the result.
Press Run to see the result. Turn the flowchart into code: inside the first branch, replace the existing line with: if main_ticket equals "fish and chips ticket" print Swap it with a friend, otherwise print Use the steak ticket.
ticket_type = "main dish"
main_ticket = "fish and chips ticket"
if ticket_type == "main dish":
print("Check the ticket")
else:
print("Use the cake ticket")Press Run to see the result. Museum ticket: anyone under 12 pays 20, a student pays 30, everyone else pays 50. Create a function ticket_price with parameters age and is_student that returns the price, then print ticket_price(10, False), ticket_price(17, True) and ticket_price(30, False) in that order.
Press Run to see the result. Create a function count_letter with parameters word and letter that returns how many times the letter appears in the word (use for). Print count_letter("assessment", "s").
Press Run to see the result. Key points
- Several arguments:
def f(a, b, c):, passed in the same number and order; none:def f():. - A function can contain if and for;
returnends it at once. - In a flowchart: diamond = if, the "no" branch = else, rectangle = an action.