Problem Statement
The company ABC has online ice-cream store. The company has released a new ice-cream and wants to know the average rating of this ice-cream. The company receives the ratings of the ice-cream on a scale of 1 to 5 from its customers.
Input
The first line of input is ratings
, space separated integer values (each rating is 1, 2, 3, 4, or 5), where each value represents the rating for the new ice-cream by a customer.
Output
Print the average rating of the ice-cream.
As is
The boilerplate code is already there to read the input from user, in the main()
function, and to print the value returned by the get_average_rating()
function. Do not edit this code.
To do ✍
get_average_rating()
has one parameter: ratings
that is a list. Write code inside the function body to find the average of the ratings, and return the average value (a float rounded to a single digit after the decimal point).
Example 1
Test input
4 3 4 4 3 4 5
Test output
3.9
Explanation
Average = Sum of ratings / number of ratings
= (4 + 3 + 4 + 4 + 3 + 4 + 5) / 7
= 27 / 7
= 3.857
= 3.9 (rounded to a single decimal point)
Useful References
Python Program
# Complete the following function
# Return the avergae rating
def get_average_rating(ratings):
# Boilerplate code - Do not edit the following
def main():
ratings = [int(x) for x in input().split()]
print(get_average_rating(ratings))
if __name__ == "__main__":
main()
Testcase 1
4 3 4 4 3 4 3 4 4 4 5 3 4
3.8
Testcase 2
5 5 5 5 5 5 5 4
4.9
Testcase 3
5 4 3 2 1 1 2 3 4 5
3.0
Testcase 4
1 1 1 1 1 1 1 1 1
1.0
# Complete the following function
# Return the avergae rating
def get_average_rating(ratings):
# Find average
avg_rating = sum(ratings) / len(ratings)
# Round to a single decimal point
avg_rating = round(avg_rating, 1)
return avg_rating
# Boilerplate code - Do not edit the following
def main():
ratings = [int(x) for x in input().split()]
print(get_average_rating(ratings))
if __name__ == "__main__":
main()