# 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;
# 再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
方法一:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 l = 100.0 2 s = 100 3 for i in range(1,11): 4 if i == 1: 5 s = l 6 else: 7 s = s + l*2 8 l = float(l/2) 9 print("第%d次,反弹%.9f米,共经过%.9f米。"%(i,l,s))
方法二:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 tour = [] 2 height = [] 3 hei = 100.0 4 5 for i in range(1,11): 6 if i == 1: 7 tour.append(hei) 8 else: 9 tour.append(hei*2) 10 hei /= 2 11 height.append(hei) 12 print("总高度:tour={0}".format(sum(tour))) 13 print("第10次反弹高度:height={0}".format(height[-1]))
format函数
list = [1,2,3,4,5,6]
print("求和{}".format(sum(list)))
输出:
求和21