Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Calculating the Average Mark with *args | Mastering Function Arguments in Python
Intermediate Python Techniques

book
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.

  1. Set the first positional argument as name in the average_mark function;
  2. For other arguments use *args;
  3. In the equation that counts the mark variable use args without *;
  4. In the print statement use name and mark variables.

Løsning

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?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
single

single

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

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt