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

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

  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.

Oplossing

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?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
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

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt