GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python 练习实例20 | 菜鸟教程
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 练习实例20题目:一球从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 米。
AI 思考中...
|
| 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. |