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

book
Challenge: Calculating the Average Mark with *args

Tehtävä

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.

Ratkaisu

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)

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 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)

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt