GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python 练习实例20 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 基础教程
Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python VS Code Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列表(List) Python 元组 Python 字典(Dictionary) Python 日期和时间 Python 函数 Python 模块 Python 文件I/O Python File 方法 Python 异常处理 Python OS 文件/目录方法 Python 内置函数

Python 高级教程

Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3​​.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验
Python 练习实例19
Python 练习实例21

Python 练习实例20

Python 100例 Python 100例

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

程序分析:无

程序源代码:

Python 实例 1

#!/usr/bin/python # -*- coding: UTF-8 -*- tour = [] height = [] initial_height = 100.0 # 初始高度 num_bounces = 10 # 弹跳次数 for i in range(1, num_bounces + 1): # 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下) if i == 1: tour.append(initial_height) else: tour.append(2 * initial_height) initial_height /= 2 height.append(initial_height) total_distance = sum(tour) final_bounce_height = height[-1] print(f"总高度:tour = {total_distance}") print(f"第{num_bounces}次反弹高度:height = {final_bounce_height}")

以上实例输出结果为:

总高度:tour = 299.609375
第10次反弹高度:height = 0.09765625

实例 2

def calculate_distance_and_height(height, times):
    total_distance = 0
    current_height = height

    for i in range(1, times + 1):
        # 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下)
        if i == 1:
            total_distance += current_height
        else:
            total_distance += current_height * 2  # 每次下落和反弹的距离
        current_height /= 2  # 反弹后的高度

    return total_distance, current_height

# 初始高度为100米,计算第10次落地时的总距离和反弹高度
height = 100
bounce_times = 10
total_distance, final_height = calculate_distance_and_height(height, bounce_times)

print(f"第{bounce_times}次落地时,共经过 {total_distance} 米。")
print(f"第{bounce_times}次反弹的高度为 {final_height} 米。")

以上实例输出结果为:

第10次落地时,共经过 299.609375 米。
第10次反弹的高度为 0.09765625 米。

Python 100例 Python 100例

AI 思考中...
Python 练习实例19
Python 练习实例21

11 篇笔记 写笔记

  1. #0

       健健

      459***[email protected]

    32

    Python3 参考方案:

    #!/usr/bin/python3
    
    hei = 100         # 总高度
    tim = 10          # 次数
    height = []       # 每次反弹高度
    for i in range(2,tim+1):  # 计算第二次落地到第十次落地
        hei /= 2
        height.append(hei)
    print('第10次落地时,反弹%s高'%(min(height)/2))        # 第十次反弹为第十次落地距离的一半
    print('第10次落地时,经过%s米'% (sum(height)*2+100))   # 总和加上第一次的 100
    

    输出结果:

    第10次落地时,反弹0.09765625高
    第10次落地时,经过299.609375米
    
    健健

       健健

      459***[email protected]

    10年前 (2017-04-22)
  2. #0

       james

      214***[email protected]

    3

    Python3 参考方案:

    #!/usr/bin/python3
    
    l=[]
    r=10
    t=100             # 第一次落地经过距离
    sum=0
    while r>1:        # 计算第二次落地到第十次落地每次的高度
        t=t/2
        r=r-1
        l.append(t)
    for k in range(0,9):       # 列表只有 9 条数据
        if k==8:
            print(l[k]/2)        # 第10次反弹高度
        sum+=l[k]
    sum=sum*2
    sum=sum+100
    print(sum)
    james

       james

      214***[email protected]

    10年前 (2017-04-26)
  3. #0

       等一个人

      252***[email protected]

    3

    Python3 测试实例:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    sum = 0
    total = 0
    for i in range(1, 10):
        sum = (100 * 2) / (2 ** i)
        total += sum
    result = 100 + total
    tenth = 100 / (2 ** 10)
    print('第10次反弹高度: {}'.format(tenth))
    print('第10次反弹后,一共经历的距离: {}'.format(result))
    
    等一个人

       等一个人

      252***[email protected]

    10年前 (2017-04-28)
  4. #0

       fancy

      249***[email protected]

    0

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    m=100
    n=input("请输入反弹次数:")
    total=[]
    l=[]
    for i in range(1,n+1):
        if i==1:
            total.append(m)
        else:
            total.append(2*m)
        m=0.5*m
        l.append(m)
    print l
    print total
    print "第%d次反弹的高度是:%f"%(n,l[n-1])
    print "第%d次落地共经过%f米"%(n,sum(total))
    
    fancy

       fancy

      249***[email protected]

    10年前 (2017-05-10)
  5. #0

       PL

      arl***[email protected]

    6

    Python3 参考方法:

    #!/usr/bin/env python3
    
    long = 100
    sum = 100
    for i in range(2, 11):
        sum = sum + long
        long = long / 2
        print("第%d次,经过%f米,反弹高度为%f" % (i, sum, long/2))
    
    PL

       PL

      arl***[email protected]

    10年前 (2017-05-23)
  6. #0

       轮子着火了

      yun***[email protected]

    4

    参考手册:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    # h 为初始高度,k 为每次弹起的高度比例,如本题弹起一半即为 0.5,n 为反弹次数
    def Sumh(h,k,n):
        L = []
        for i in range(1,n+1):
            h *= k
            totalh = h * 3
            L.append(totalh)
        print h
        print sum(L) - h  # 第 10 次落地高度,要去除最后一次反弹
    Sumh(100,0.5,10)
    
    轮子着火了

       轮子着火了

      yun***[email protected]

    10年前 (2017-05-23)
  7. #0

       Think-dfrent

      iwa***[email protected]

    0

    Python3 参考方法:使用递归生成每一次反弹的高度,其中参数为第几次反弹,如n=1时,第一次反弹高度为height(1)=50

    #!/usr/bin/python3
    
    def height(n):
        if n==0 :
            return 100
        else:
            return height(n-1)/2
    
    sum=0
    count=10
    for i in range(0,count):
        if i==0:
            sum=sum+height(i)
        else:
            sum=sum+2*height(i)
        #print(height(i))
    
    print(sum)
    print(height(10))
    Think-dfrent

       Think-dfrent

      iwa***[email protected]

    10年前 (2017-06-16)
  8. #0

       Mervyn

      110***[email protected]

    0

    参考方法:

    a = 100.00
    b=0.0
    print a/(2**10)
    for i in range(0,10):
        b,a = b+2*a, a/2
    print b -100
    
    Mervyn

       Mervyn

      110***[email protected]

    9年前 (2017-08-16)
  9. #0

       小差罗

      675***[email protected]

    0

    Python3 测试实例:

    h=100
    t=10
    height=[100]
    for i in range(t):
        height.append(h)
        h=h/2
    print(height)
    print('总高度:',sum(height[:10]),'第10次反弹高度 height[10]:',height[10]/2)
    小差罗

       小差罗

      675***[email protected]

    9年前 (2017-09-12)
  10. #0

       如花

      yiy***[email protected]

    1

    参考方法:

    from __future__ import division
    
    height = 100
    n = 10
    tour = 0
    psum = pow(2,10)
    bnce10 = 100/psum
    print bnce10
    for i in range(1,10):
        #print tour
        tour += 2*(100/pow(2,i))
    tour = height + tour
    print "tour=%f"%tour
    如花

       如花

      yiy***[email protected]

    9年前 (2017-09-13)
  11. #0

       忧伤的中指

      fin***[email protected]

    0

    兼容 Python3.x 与 Python2.x:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import math
    
    # 设落地n次
    
    n = int(input("请输入反弹次数:"))
    height = 100
    print("第  {} 次落地共: {:<8}米".format(n,height*(3-math.pow(2,-(n-2)))))
    print("第  {} 次  反  弹: {:<8}米".format(n,height*math.pow(2, -(n))))
    忧伤的中指

       忧伤的中指

      fin***[email protected]

    9年前 (2017-10-20)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.