markdown 如何计算给定列表中的数字平均值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 如何计算给定列表中的数字平均值相关的知识,希望对你有一定的参考价值。
## Python Program to Calculate the Average of Numbers in a Given List
This is a Python Program to Calculate the Average of Numbers in a Given List.
### Problem Description
The program takes the elements of the list one by one and displays the average of the elements of the list.
### Problem Solution
1. Take the number of elements to be stored in the list as input.
2. Use a for loop to input elements into the list.
3. Calculate the total sum of elements in the list.
4. Divide the sum by total number of elements in the list.
5. Exit.
### Source Code
### To Find Average By Using Built-in Function
```python
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
avg = sum(numbers)/n
print("The created list: ",numbers)
print("The average = ",avg)
```
### Program Explanation
1. User must first enter the number of elements which is stored in the variable n.
2. The value of I ranges from 0 to the number of elements and is incremented each time after the body of the loop is executed.
3. Then, the element that the user enters is stored in the variable elem.
4. a.append(elem) appends the element to the list.
5. Now the value of i is incremented to 2.
6. The new value entered by the user for the next loop iteration is now stored in elem which is appended to the list.
7. The loop runs till the value of i reaches n.
8. sum(a) gives the total sum of all the elements in the list and dividing it by the total number of elements gives the average of elements in the list.
9. round(avg,2) rounds the average upto 2 decimal places.
10. Then the average is printed after rounding.
### Test Case-1(Without Fixed Decimal Points)
```python
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
avg = sum(numbers)/n
print("The created list: ",numbers)
print("The average = ",avg)
```
Enter the number of elements: 4
Enter the elements: 30
Enter the elements: 40
Enter the elements: 10
Enter the elements: 20
The created list: [30, 40, 10, 20]
The average = 25.0
### Test Case-2(Fixed Decimal Points)
```python
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
avg = sum(numbers)/n
print("The created list: ",numbers)
print("The average = ",round(avg, 2))
```
Enter the number of elements: 4
Enter the elements: 55
Enter the elements: 80
Enter the elements: 23
Enter the elements: 56
The created list: [55, 80, 23, 56]
The average = 53.5
### To Find Average Without Built-in Function
```python
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findAvg(numbers):
total = 0
for j in numbers:
total += j
avg = total/n
return avg
if __name__ == '__main__':
temp = findAvg(numbers)
print("The Average =",temp)
```
### Test Case-1(Without Fixed Decimal Points)
```python
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findAvg(numbers):
total = 0
for j in numbers:
total += j
avg = total/n
return avg
if __name__ == '__main__':
temp = findAvg(numbers)
print("The Average =",temp)
```
Enter the number of elements: 4
Enter the elements: 88
Enter the elements: 70
Enter the elements: 90
Enter the elements: 70
The Average = 79.5
### Test Case-2(Fixed Decimal Points)
```python
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findAvg(numbers):
total = 0
for j in numbers:
total += j
avg = total/n
return avg
if __name__ == '__main__':
temp = findAvg(numbers)
print("The Average =",round(temp, 2))
```
Enter the number of elements: 4
Enter the elements: 55
Enter the elements: 60
Enter the elements: 78
Enter the elements: 90
The Average = 70.75
Jubayer Hossain
> Please share your tips or tricks for solving this problem in a better way! **Happy Coding!**
以上是关于markdown 如何计算给定列表中的数字平均值的主要内容,如果未能解决你的问题,请参考以下文章