|
| 1 | + |
| 2 | +## 2.3: Average of numbers |
| 3 | + |
| 4 | +**S-1: The problem** |
| 5 | +Take numbers from a user and show the average of the numbers the user entered. |
| 6 | + |
| 7 | +<details> |
| 8 | + <summary><b>S-2: Click Here For Show Hints</b></summary> |
| 9 | + <p>To solve this problem. |
| 10 | + |
| 11 | +First, ask the user - How many numbers you want to enter? |
| 12 | + |
| 13 | +Then, run a for-loop. Each time, take input from the user and put it in a list. |
| 14 | + |
| 15 | +Once you get all the numbers, you can send the list to the sum function. The sum function will add all the numbers and give you the total. |
| 16 | + |
| 17 | +Finally, divide the total by the number of elements the user entered. |
| 18 | + |
| 19 | +That’s it, you will get the answer. |
| 20 | + |
| 21 | +Want to try it yourself first? Go to the code editor and try it. |
| 22 | +</p> |
| 23 | + </details> |
| 24 | +<br> |
| 25 | + |
| 26 | +**[Try It:](/#)** |
| 27 | + |
| 28 | + |
| 29 | +#### S-3: Solution |
| 30 | +```python |
| 31 | +len = int(input("How many numbers do you want to enter? ")) |
| 32 | + |
| 33 | +nums = [] |
| 34 | + |
| 35 | +for i in range(0, len): |
| 36 | + element = int(input("Enter element: ")) |
| 37 | + nums.append(element) |
| 38 | + |
| 39 | +total = sum(nums) |
| 40 | +avg = total/len |
| 41 | +print("Average of elements you entered",round(avg,2)) |
| 42 | +``` |
| 43 | + |
| 44 | +**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** |
| 45 | + |
| 46 | +#### S-4: Explanation |
| 47 | +First, ask the user how many numbers he/she wants to enter. Once we have the number, run a for loop. To collect the numbers. |
| 48 | + |
| 49 | +While collecting the numbers, we are adding those in the list called nums. |
| 50 | + |
| 51 | +Then we pass the list to the sum function. The sum function returns us the sum of each number in the list |
| 52 | + |
| 53 | +Eventually, we divide the total by the number of elements to get the average. |
| 54 | + |
| 55 | +#### S-5: Another Solution |
| 56 | +```python |
| 57 | +len=int(input("How many numbers you want to enter: ")) |
| 58 | + |
| 59 | +total = 0 |
| 60 | + |
| 61 | +for i in range(0,len): |
| 62 | + elem=int(input("Enter element: ")) |
| 63 | + total += elem |
| 64 | + |
| 65 | +avg = total/len |
| 66 | +print("Average of elements you entered",round(avg,2)) |
| 67 | +``` |
| 68 | +**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** |
| 69 | + |
| 70 | +#### S-6: Explanation |
| 71 | +In the second approach, other than directly adding to the list, we are adding it to the total variable. And then we are dividing it by the number of input the user entered. |
| 72 | + |
| 73 | +#### S-7: Take Away |
| 74 | +> To get the average, calculate the total and divide by the number of elements. |
| 75 | +
|
| 76 | + |
| 77 | +[](../README.md) |
| 78 | + |
| 79 | + |
| 80 | +###### tags: `programmig-hero` `python` `float` `int` `math` |
0 commit comments