53
Question Write a program using user-defined function to calculate and display average of all the elements in a user-defined tuple containing numbers.
def calculate_average(num):
total = 0
for element in num:
total += element
average = total / len(num)
return average
n = eval(input("Enter the tuple: "))
average = calculate_average(n)
print("The average of the elements in the tuple is:", average)Enter the tuple: (2, 4, 6, 8, 10)
The average of the elements in the tuple is: 6.0
def
calculate_average
(
num
):
total
=
0
for
element
in
num
:
total
+=
element
average
=
total
/
len
(
num
)
return
average
n
=
eval
(
input
(
"Enter the tuple: "
))
average
=
calculate_average
(
n
)
print
(
"The average of the elements in the tuple is:"
,
average
)
Output
Enter the tuple: (2, 4, 6, 8, 10)
The average of the elements in the tuple is: 6.0