我编写了一个代码,用于记录运行速度值(min / km),将其转换为速度(km / hr),然后根据坡度梯度以及行进方向是上升还是下坡来降低速度计算(km / hr)。然后显示新的运行速度以及新的运行速度和路线改变的时间。
问题是当我输入速度如3:50(min / km),上坡速度为1%时,新的跑步速度为3:60(min / km)。在这种情况下,如何让脚本勾选到4:00?此外,如果输入3:55(min / km),则给定的新运行速度为4:5(min / km),其读数应为4:05(min / km)。我该如何编辑?
脚本是:
import math
print('Q1')
SurveyPace = input("Running Pace (min/km): \n "). split(":")
SurveyPace = float(SurveyPace[0])*60 + float(SurveyPace[1])
Speed = 3600/SurveyPace
print("Original running speed =","%.2f" % round(Speed,2), 'km/hr')
print("--------------------------------------------------------")
print('Q2')
SlopeDirection = int(input('For Uphill press 1 \nFor Downhill press 2 \n '))
print("--------------------------------------------------------")
print('Q3')
SlopeGradient = float(input('Percentage gradient(without the % symbol)?\n '))
print("--------------------------------------------------------")
print('CALCULATED RESULTS')
print("--------------------------------------------------------")
if SlopeDirection == 1:
Change = - 0.65 * SlopeGradient
if SlopeDirection == 2:
Change = + 0.35 * SlopeGradient
print ('This route alters your speed by \n', Change,'km/hr')
print("--------------------------------------------------------")
AdjustedSpeed = Speed + Change
AdjustedPace = 3600/AdjustedSpeed
PaceSecs = round(AdjustedPace % 60)
PaceMins = math.floor(AdjustedPace/60)
print("New running speed is \n","%.2f" % round(AdjustedSpeed,2), 'km/hr')
print("--------------------------------------------------------")
print("New running pace is \n", str(PaceMins) + ":" + str(PaceSecs), 'min/km')
print("--------------------------------------------------------")
print("This route alters your pace by \n", int(PaceSecs + (PaceMins*60)) - SurveyPace, "sec/km") #Prints the time change incured
print("--------------------------------------------------------")
由于
答案 0 :(得分:0)
您可以使用内置函数divmod
执行此操作:
# Round the AdjustedPace to seconds
AdjustedPace = round(3600/AdjustedSpeed)
minutes, seconds = divmod(AdjustedPace, 60)
print(minutes)
print(seconds)
这将导致:
#Pace = 3:50
#4
#0
#Pace = 3:55
#4
#5
答案 1 :(得分:0)
我会使用datetime中的timedelta
个对象执行此操作:
import datetime
inp = raw_input('Enter your pace in minutes per km (min:km):')
mins, kms = inp.split(':')
time = datetime.timedelta(minutes=int(mins))
例如,如果您输入60分钟,则会给您:
> time
datetime.timedelta(0, 3600)
然后你可以对它进行数学运算并保持正确:
> time / 2
datetime.timedelta(0, 1800)
或者,如果你想要分钟只需将它除以60,小时除以3600.你还可以相互添加和减去timedelta对象,或者如果你想要时间戳,还可以从datetime对象中减去。或者如果你的除数留下余数:
> new = time / 17
> new
datetime.timedelta(0, 3600)
> new.seconds
200
> new.microseconds
764706
如果您愿意,可以使用它来进行舍入。这是确保您的时间始终保持准确的好方法。