Calculate Average
Calculate Average
def calculate_average(numbers):
"""
Args:
Returns:
"""
Return 0
Total = 0
Total += number
Return average
Code:
def calculate_average(numbers):
"""
Args:
Returns:
"""
return 0
total = 0
total += number
return average
# Test the function with a predetermined list of numbers
average_result = calculate_average(number_list)
Execution Example:
1. number_list = [10, 20, 30, 40, 50]: A list of numbers is created and assigned to the variable
number_list.
• Inside calculate_average:
* numbers = [10, 20, 30, 40, 50]: The numbers parameter receives the value of number_list.
* if not numbers:: The code checks if the numbers list is empty. Since it's not empty, the if
condition is false.
* total = 0: A variable total is initialized to 0. This variable will store the sum of the numbers.
* for number in numbers:: A for loop begins iterating through each number in the numbers list.
* average = total / len(numbers): After the loop completes, the code calculates the average. total
is 150, and len(numbers) is 5 (the number of elements in the list).
* return average: The function returns the calculated average (which is 30.0).
3. average_result = 30.0: The average_result variable now holds the value returned by the function
(30.0).
4. print("The average of the numbers is:", average_result): The code prints the following output to the
console:
```
```