經過用戶輸入一組數據(用戶一個一個輸入),而後計算數據的中位數/平均值/方差/求和app
nums = [] while True: num1 = input('請輸入你須要輸入的數字(輸入 q 退出):') if num1 == 'q': break nums.append(int(num1))
def get_sum(nums): count = 0 for i in nums: count += int(i) return count count = get_sum(nums) print('count:',count)
def get_average(nums): count = get_sum(nums) average_count = count/len(nums) return average_count average_count = get_average(nums) print(average_count)
def get_variance(nums): average_count = get_average(nums) variance_count = 0 for i in nums: variance_count += pow(i-average_count,2) return variance_count variance_count = get_variance(nums) print(variance_count)
def get_median(nums): # ['123', '32', '123', '234'] # 0 1 2 3 nums.sort() nums_len = len(nums) if nums_len % 2 == 0: print((nums[int(nums_len / 2 - 1)] + nums[int(nums_len / 2)]) / 2) else: print(nums[nums_len // 2]) get_median(nums)