0% found this document useful (0 votes)
3 views

Calculate Average

The document provides a Python function, calculate_average, that computes the average of a list of numbers using a for loop. It includes error handling for empty lists and demonstrates the function's execution with a sample list of numbers, resulting in an average of 30.0. The code is well-structured with clear comments explaining each step of the process.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Calculate Average

The document provides a Python function, calculate_average, that computes the average of a list of numbers using a for loop. It includes error handling for empty lists and demonstrates the function's execution with a sample list of numbers, resulting in an average of 30.0. The code is well-structured with clear comments explaining each step of the process.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Coding structure

def calculate_average(numbers):

"""

Calculates the average (mean) of a list of numbers using a for loop.

Args:

Numbers: A list of numbers (integers or floats).

Returns:

The average (mean) of the numbers in the list.

Returns 0 if the list is empty to avoid division by zero.

"""

If not numbers: # Check if the list is empty

Return 0

Total = 0

For number in numbers:

Total += number

average = total / len(numbers)

Return average

# Test the function with a predetermined list of numbers

number list = [10, 20, 30, 40, 50]

average result = calculate average(number list)

print("The average of the numbers is:", average result)


Let’s illustrate the execution of the calculate_average function with the provided example:

Code:

def calculate_average(numbers):

"""

Calculates the average (mean) of a list of numbers using a for loop.

Args:

numbers: A list of numbers (integers or floats).

Returns:

The average (mean) of the numbers in the list.

Returns 0 if the list is empty to avoid division by zero.

"""

if not numbers: # Check if the list is empty

return 0

total = 0

for number in numbers:

total += number

average = total / len(numbers)

return average
# Test the function with a predetermined list of numbers

number_list = [10, 20, 30, 40, 50]

average_result = calculate_average(number_list)

print("The average of the numbers is:", average_result)

Execution Example:

1. number_list = [10, 20, 30, 40, 50]: A list of numbers is created and assigned to the variable
number_list.

2. average_result = calculate_average(number_list): The calculate_average function is called with


number_list as the argument.

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

* 1st Iteration: number = 10

* total += number ( total = 0 + 10 = 10)

* 2nd Iteration: number = 20

* total += number ( total = 10 + 20 = 30)

* 3rd Iteration: number = 30


* total += number ( total = 30 + 30 = 60)

* 4th Iteration: number = 40

* total += number ( total = 60 + 40 = 100)

* 5th Iteration: number = 50

* total += number ( total = 100 + 50 = 150)

* 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).

* average = 150 / 5 = 30.0

* 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:

```

The average of the numbers is: 30.0

```

You might also like