Python 練習實例20
題目:一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米?第10次反彈多高?
程式分析:無
程式源代碼:
Python 實例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
tour = []
height = []
hei = 100.0 # 起始高度
tim = 10 # 次數
for i in range(1, tim + 1):
# 從第二次開始,落地時的距離應該是反彈高度乘以2(彈到最高點再落下)
if i == 1:
tour.append(hei)
else:
tour.append(2*hei)
hei /= 2
height.append(hei)
print('總高度:tour = {0}'.format(sum(tour)))
print('第10次反彈高度:height = {0}'.format(height[-1]))
以上實例輸出結果為:
總高度:tour = 299.609375 第10次反彈高度:height = 0.09765625