def中return和print的区别
def average(*args):
sum = 0.0
if len(args) == 0:
return sum
for x in args:
sum = sum + x
return sum / len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
这样可以print三个正确答案,
def average(*args):
sum = 0.0
if len(args) == 0:
print sum
for x in args:
sum = sum + x
print sum / len(args)
average()
average(1, 2)
average(1, 2, 2, 3, 4)
这样把def中的return改成print的话只能print一个结果,也就是0.0。
这是为什么呢?def中用return和print,除了打印不打印出来结果,还有别的区别吗?