美文网首页
Python 练习实例20

Python 练习实例20

作者: loinue | 来源:发表于2022-03-27 08:40 被阅读0次

来自菜鸟教程
https://www.runoob.com/python/python-exercise-example20.html
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

程序分析:无

程序源代码:

#!/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

相关文章

网友评论

      本文标题:Python 练习实例20

      本文链接:https://www.haomeiwen.com/subject/zfokjrtx.html