利用python去实现数学基本值的计算

python

def get_nums():

nums = []#获取列表

num = input(\'请输入数字:\').strip()

while num != \'\':

nums.append(num)#添加数字

num = input(\'请输入数字:\').strip()#继续输入数字

return nums#返回给nums

print(get_nums())#将列表显示出来

nums = get_nums()

print(nums)

def get_len(nums):#求长度

count =0

for i in nums:

count += 1

return count

nums = get_nums()#注意此时一定要左对齐,不然会报错,显示nums没有被定义

print(get_len(nums))

def get_add(nums):#求和

sum = 0

for i in nums:

sum += eval(i)#把字符串i变成数字后才能相加

return sum

nums = get_nums()

print(get_add(nums))

def get_mean(nums):#求平均数

sum = 0

for i in nums:

sum += eval(i)

mean = sum / get_len(nums)

return mean

nums = get_nums()

print(get_mean(nums))

def get_var(nums,mean):#求方差,方差的定义是每个变量与平均数差的平方和除以变量的个数

sum = 0

for i in nums:

sum += (mean-eval(i))**2

var = sum / get_len(nums)

return var

nums = get_nums()

mean = get_mean(nums)

print(get_var(nums,mean))

l1=[1, 2, 3, 4, 5, 6]

def get_median(nums):

nums_sort = sorted(nums)#python内置,对所有的对象进行排序操作

size = get_len(nums)

if size % 2 == 0:

med = (eval(nums_sort[size // 2 -1]) + eval(nums_sort[size // 2])) /2

else:

med = eval(nums_sort[size // 2])

return med

nums = get_nums()

print(get_median(nums))

def run():

nums = get_nums()

l1 = [\'长度\',\'和\',\'平均值\',\'方差\', \'中位数\']

while True:

print("欢迎来到数学计算器".center(50,"-"))

print("""

1、长度

2、求和

3、求平均值

4、求方差

5、求中位数

6、退出程序

""")

choice = input("请输入您的需求").strip()

result = 0

if choice == "1":

result = get_len(nums)

elif choice == "2":

result = get_add(nums)

elif choice == "3":

result = get_mean(nums)

elif choice == "4":

result = get_var(nums,get_mean(nums))

elif choice == "5":

result = get_median(nums)

elif choice == "6":

print(\'退出程序\')

return

else:

print(\'输入格式有误\')

print(f"{l1[eval(choice) - 1]}:{result}")

run()




以上是 利用python去实现数学基本值的计算 的全部内容, 来源链接: utcz.com/z/388281.html

回到顶部