Python Arena
Step 4 of 5

Several parameters

A function can have several parameters, separated by commas. A call then gives as many arguments, and they're matched by position: the first argument goes to the first parameter, the second to the second, and so on.

def introduce(name, age):
    print(f"{name} is {age} years old.")


introduce("Mia", 12)
introduce(40, "Leo")
Output
Mia is 12 years old.
40 is Leo years old.

The second call has its arguments the wrong way round. Python doesn't know what they mean; it simply gives the first one to name. Give the arguments in the order of the header.

Helper variables and decisions

A function's body can have its own helper variables, ifs and loops, like any program:

def compare(a, b):
    difference = a - b
    if difference > 0:
        print(f"{a} is {difference} more than {b}")
    elif difference < 0:
        print(f"{a} is {-difference} less than {b}")
    else:
        print(f"{a} and {b} are equal")


compare(10, 4)
compare(3, 8)
compare(5, 5)
Output
10 is 6 more than 4
3 is 5 less than 8
5 and 5 are equal

difference is made inside the function, every time it runs. It belongs to the function, and the rest of the program can't see it.

The names belong to the function

The names of the parameters have nothing to do with variables outside the function, even when the names are the same. Here the main program has variables called width and height too:

def area(width, height):
    print(f"A {width} by {height} rectangle has an area of {width * height}")


width = 2
height = 3
area(10, 5)
area(height, width)
Output
A 10 by 5 rectangle has an area of 50
A 3 by 2 rectangle has an area of 6

In the first call, width inside the function is 10, whatever the width outside holds. In the second call, the first argument is the outside height, 3, so inside the function width is 3. Only the order of the arguments counts, never the names in the call.

Your task

Challenge 1 of 3

What does this program print? Type each line it prints.

Challenge 2

Write a function times_table(number, count) that prints the first count lines of the multiplication table of number. A call times_table(3, 4) prints:

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12

Challenge 3

Write a function report(points, maximum) that prints the points out of the maximum and the percentage, as a whole number rounded down, as below. On the next line it prints Passed if the percentage is at least 50, and Try again otherwise. A call report(45, 60) prints:

45/60 points (75%)
Passed