Challenge: Calculating the Average Mark with *args
Oppgave
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.
Løsning
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)
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 2. Kapittel 3
single
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)
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår