关于变量的数据类型的问题
def average(*args): if len(args)==0: return 0.0 else: sum = 0.0 for a in args: sum += a return sum/len(args) print average() print average(1, 2) print average(1, 2, 2, 3, 4) 为什么sum要定义成0.0,定义为0就错误
def average(*args): if len(args)==0: return 0.0 else: sum = 0.0 for a in args: sum += a return sum/len(args) print average() print average(1, 2) print average(1, 2, 2, 3, 4) 为什么sum要定义成0.0,定义为0就错误
2016-04-06
#!/usr/bin/python # -*- coding: utf-8 -*- import sys def average(*args): if len(args)==0: return 0 else: sum = 0 for a in args: sum += a return sum/len(args) a = average() b = average(1, 2) c = average(1, 2, 2, 3, 4) print (a) print (b) print (c)
结果如下
0
1.5
2.4
我在 http://www.mcqyy.com/RunCode/python3/ 这上面运行的,并没有出现你说的问题(两个0 两个0.0 或者一个0一个0.0我都试过 没问题)
举报