我错过了什么来获得会议记录?

2024-04-26 13:59:15 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我想我只是想得太多了。代码的其余部分是我想要的(它做我想要它做的),但是我似乎不知道如何计算分钟数。 所以基本上我应该从用户那里得到“英里驱动”和“英里每小时”。然后,我的程序应该确定他们在两条不同的线路上走这段距离需要多长时间,以小时+分钟为单位(就像一条线路上的“小时:”和另一条线路上的“分钟:”一样)。我已经计算出了“小时”,但我不能计算出分钟。我觉得这真的很简单,我只是错过了。你知道吗

print("\nTravel Time Calculator")

miles = float(input("Enter Miles: "))
milesPh = float(input("Enter Miles Per Hour: "))

print("\nEstimated Travel Time")

if miles <= 0:
    print("Miles must be greater than zero. Please try again.")
elif milesPh <= 0:
    print("Miles per hour must be greater than zero. Please try again.")
else:
    # calculate and display travel time
    hours = round(miles / milesPh)
    print("Hours: " + str(hours))
    minutes = round()
    print("Minutes: " + str(minutes))

Tags: inputtimebefloat线路printenter小时
2条回答

通过潜水miles / milesPh,您可以很容易地获得所花费的总时间。你的问题是你做得太早了。你知道吗

假装miles / milesPh = 5.5。那是五个半小时。如果你马上把它修好,你就失去了最后半个小时。你知道吗

所以基本上,你必须有一个系统,在你把小时数四舍五入之前,计算出分钟数。有两种直观的方法:

(1)计算它所指的分钟数,并据此计算小时数:

total_minutes = (miles / milesPh) * 60  # total time in minutes
hours = total_minutes // 60             # integer division by 60 (drop the remainder)
minutes = int(total_minutes) % 60       # remainder after integer division by 60

(2)分别计算小时数和分钟数:

total_time = miles / milesPh              # total time in hours
hours = int(total_time)                   # the integer part of total time is hours
minutes = int((total_time - hours) * 60)  # the decimal part of total time becomes minutes

试试这个:

print("\nTravel Time Calculator")

miles = float(input("Enter Miles: "))
milesPh = float(input("Enter Miles Per Hour: "))

print("\nEstimated Travel Time")

if miles <= 0:
    print("Miles must be greater than zero. Please try again.")
elif milesPh <= 0:
    print("Miles per hour must be greater than zero. Please try again.")
else:
    # calculate and display travel time
    hours = int(miles / milesPh)
    print("Hours: " + str(hours))
    minutes = ((miles / milesPh)*60) % 60
    print("Minutes: " + str(minutes))

输出

Travel Time Calculator
Enter Miles: 20
Enter Miles Per Hour: 12

Estimated Travel Time
Hours: 1
Minutes: 40.0

相关问题 更多 >