# Enter a code
def average(*args):
lenth = len(args)
if lenth == 0 :
return 0;
else:
sum = 0
for item in args:
sum += item
avg = sum / lenth
return avg
print(average())
print(average(1,2))
因为你的sum = 0,意思是打印的最后结果默认为整数,变成sum = 0.0即可
是不是python版本问题,我用Python 3.9.1没问题,输出是1.5
sum = 0.0就行了
已经解决了,
# Enter a code
def average(*args):
lenth = len(args)
if lenth == 0 :
return 0;
else:
sum = 0
for item in args:
sum += item
avg = float(sum) /float( lenth) #将两个数字转为float就不丢失小数部分
return avg
print(average())
print(average(1,2))