After learning the knowledge of Python related data types and functions, I used string segmentation to implement a small program that inputs any multiple data and calculates their average. The idea is to receive the input string, use spaces as delimiters, store the divided data in a list (lst1), transfer the data in lst1 to another empty list (lst), and convert the string when transferring It is an integer type, so the function can be used to find the sum and average of the numbers in lst.
print("-----求平均值,可输入任意多个数-------") lst = [] #定义一个空列表 str = raw_input("请输入数值,用空格隔开:") lst1 = str.split(" ")#lst1用来存储输入的字符串,用空格分割 i = 0 while i <= len(lst1)+1: lst.append(int(lst1.pop()))#将lst1的数据转换为整型并赋值给lst i += 1 #print(lst) def sum(list): "对列表的数值求和" s = 0 for x in list: s += x return s def average(list): "对列表数据求平均值" avg = 0 avg = sum(list)/(len(list)*1.0) #调用sum函数求和 return avg print("avg = %f"%average(lst))
Run result:
Please enter the value, separated by spaces: 21 32 45 65 avg = 47.333333
The above is the detailed content of How to find average using python. For more information, please follow other related articles on the PHP Chinese website!