用户输入随机个数的数字,输入的数两两相加,将最终结果添加在字典中。
需要用到reduce函数,因此第一步导入
from functools import reduce
然后设置两两相加的函数f()
def f(x,y):
return x+y
接下来设计输入系统
dic={'最终结果':None,}
L=[]
while True:
a=input('请输入一个数,结束请输Q.')
if a=='Q':
break
elif a.isdigit(): #isdigit()判断字符串是否由数字组成
a=int(a)
L.append(a)
else:
print('请重新输入数字')
dic['最终结果']=reduce(f,L)
print(dic)
欧了~(~ ̄▽ ̄)~