Challenge: Calculating the Average Mark with *args
Taak
Swipe to start coding
Let's calculate the average mark for the student. Each student has a different number of marks, so we don't know how many arguments will be passed to the function.
- Set the first positional argument as
name
in theaverage_mark
function; - For other arguments use
*args
; - In the equation that counts the
mark
variable useargs
without*
; - In the
print
statement usename
andmark
variables.
Oplossing
9
1
2
3
4
5
6
7
def average_mark(name, *args):
mark = round(sum(args)/len(args), 1) # average value rounded to the 1 num after the dot
print(f"{name} got {mark}")
average_mark("Tom", 4.0, 3.5, 3.0, 3.3, 3.8)
average_mark("Dick", 2.5, 3.8)
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 2. Hoofdstuk 3
9
1
2
3
4
5
6
7
def average_mark(___, ___):
mark = round(sum(___)/len(___), 1) # average value rounded to the 1 num after the dot
print(f"{___} got {___}")
average_mark("Tom", 4.0, 3.5, 3.0, 3.3, 3.8)
average_mark("Dick", 2.5, 3.8)
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.