33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
def add(a=0, b=0, *args):
|
|
total = a + b
|
|
for num in args:
|
|
total += num
|
|
return total
|
|
|
|
"""
|
|
Adds two or more numbers together.
|
|
|
|
Parameters:
|
|
a (int or float, optional): The first number to add. Defaults to 0.
|
|
b (int or float, optional): The second number to add. Defaults to 0.
|
|
*args (int or float): Additional numbers to add.
|
|
|
|
Returns:
|
|
int or float: The sum of all provided numbers.
|
|
"""
|
|
|
|
print(add(1, 2)) # Output: 3
|
|
print(add(1, 2, 3, 4)) # Output: 10
|
|
print(add(1)) # Output: 1
|
|
print(add(1, 2, 3, 4, 5, 6)) # Output: 21
|
|
print(add()) # Output: 0
|
|
|
|
# Function to calculate the average mark of a student
|
|
# It takes the student's name and a variable number of marks as arguments.
|
|
def average_mark(name, *args):
|
|
mark = round(sum(args)/len(args), 2) # average value rounded to the 2 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)
|
|
average_mark("Harry", 4.0, 4.5, 3.8, 4.2, 4.0, 3.9) |