hts95
hts95

Reputation: 123

What am I missing to get the minutes?

Okay, so I think I'm just overthinking this way too much. The rest of the code is how I want it (it does what I want it to do), but I can't seem to figure out how to get the minutes calculated. So basically I'm supposed to get "miles driven" and "miles per hour" from the user. Then my program is supposed to determine how long it would take them to go that distance in hours + minutes on two separate lines (so like "hours:" on one line and "minutes:" on the other. I have the "hours" figured out, but I can't figure out minutes. I feel like it's really simple and I'm just missing it.

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))

Upvotes: 0

Views: 191

Answers (2)

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

You can get the total time taken really easily by diving miles / milesPh. Your problem is that you're rounding it off too early.

Pretend that miles / milesPh = 5.5. That's five and a half hours. If you round it off immediately, you lose the last half-hour.

So basically, you have to have some system to get the number of minutes out of this, before you round off the number of hours. There are two intuitive ways to go about this:

(1) Calculate the number of minutes it refers to, and calculate the number of hours based on that:

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) Calculate the number of hours and number of minutes separately:

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

Upvotes: 1

GOVIND DIXIT
GOVIND DIXIT

Reputation: 1828

Try this:

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))

Output

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

Estimated Travel Time
Hours: 1
Minutes: 40.0

Upvotes: 1

Related Questions