- 3
How to find average of a series of number entered by a user
How do I find the average of a series of a number entered by a user. The program should first prompt the user for how many numbers contained in the series . Note: the average should always be a float, even if the user inputs all integer
6 Antworten
+ 6
Why do you need to tell the computer how many numbers will be entered? The computer can count.
nums = input().split()
print(sum(int(n) for n in nums) / len(nums))
+ 4
Average = Sum of observation / Total no. of observation
Though it depends on how the input will look like:
If the input is like this:
2.0 3.1 1.2 1.5
Then you can use .split() to split string into list and use list comprehension to convert each element to float.
+ 2
initialize a count and a total variables to zero
loop while user give entry:
add user input casted to float to total
increment count
print average = total/count
- 3
please be more specific, show an example