我的任务是编写一个规划田径跑步的函数。我必须添加一个 while 循环来查看输入参数 a_distance 是否被覆盖,并计算该距离内的圈数。我定义了以下(尚未完成)函数:
import math # ceil
LAP_DISTANCE = 400
def plan_track_run(a_distance):
count = 0
total = 0
r_output = ""
while total < a_distance:
count = count + (1/(LAP_DISTANCE)*a_distance)
total = total + count * LAP_DISTANCE
r_output += f"lap #{math.ceil(count)} time ____ s in {count:.2f} x {LAP_DISTANCE}m ={total:.2f}m\n"
输出是一个模板,用于记录特定距离的时间。当 a_distance 小于一圈距离(400 米)时的输出可以在这里看到:
lap #1 time ____ s in 0.50 x 400m = 200.00m
不幸的是,当 a_distance 大于一圈距离时,例如,我没有达到预期的输出。 1500米。每圈应该有一个多行输出,向上计数,直到最后一行显示总圈数和总距离:
lap #1 time ____ s in 1.00 x 400m = 400.00m
lap #2 time ____ s in 2.00 x 400m = 800.00m
lap #3 time ____ s in 3.00 x 400m = 1200.00m
lap #4 time ____ s in 3.75 x 400m = 1500.00m
我当前的 a_distance = 1500 的错误输出是:
lap #4 time ____ s in 3.75 x 400m = 1500.00m
请帮助我修改此代码,以便我在距离大于 400 米的情况下也能达到所需的输出。预先感谢您。
LAP_DISTANCE = 400
a_distance = 1500
count = count + (1/(LAP_DISTANCE)*a_distance) # 0 + 1/400*1500 = 3.75
total = total + count * LAP_DISTANCE # 0 + 3.75 * 400 = 1500
你的第二个公式完全抵消了第一个公式,留下
total == a_distance
这将终止循环。
为什么需要根据距离来计算计数?只要
count = count + 1
就足够了,不是吗?