python3练习100题——020

原题链接:http://www.runoob.com/python/python-exercise-example20.html

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

我的代码:

def ball():
    times=int(input("Hou many times the ball hit the floor?"))
    h=100.0
    record=[]      
    length=100
    for i in range(0,times):
        h=h/2
        record.append(h)
    for i in record[:-1]:
        length += i*2
    print(length)
    print(record[-1])

if __name__ == '__main__':
    ball()

思考:

题目本身很容易,但是要搞清楚循环多少次。10次落地时,总路径长度计算的是100+9次来回的路径长度。

猜你喜欢

转载自www.cnblogs.com/drifter/p/9149562.html