high = 100
n = 10
high_all = 0 #第n次落地時走過的長度
high_each = 0 #每次落地的高度
def ball_lands(n):
global high_all, high_each, high
#小球第一次落地時
if n == 0:
high_each = high
high_all += high_each
#print("1", high_each, high_all)
return high_each
#從小球第n次落地往前推
high_each = high * (1 / 2) ** n
print(u"第%d次小球彈起的高度為:%.2f" %(n, high_each))
high_all += high_each * 2
#print("2", high_each, high_all)
ball_lands(n - 1)
ball_lands(n)
print(u"小球落地%d次,共經過%.2f米。" % (n,high_all))
high = 100
n = 10
high_all = 0 #第n次落地時走過的長度
high_each = 0 #每次落地的高度
def ball_lands(n):
global high_all, high_each, high
#小球第一次落地時
if n == 0:
high_each = high
high_all += high_each
#print("1", high_each, high_all)
return high_each
#從小球第n次落地往前推
high_each = high * (1 / 2) ** n
print(u"第%d次小球彈起的高度為:%.2f" %(n, high_each))
high_all += high_each * 2
#print("2", high_each, high_all)
ball_lands(n - 1)
ball_lands(n)
print(u"小球落地%d次,共經過%.2f米。" % (n,high_all))