Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converter
#1
I wanted to make converter from Miles to Km and have estimated driving time in my program . The problem is i cant not print exactly driving time .
if i use //, instead of /. This forces Python to do an integer division (i.e. ignore decimal points) since we ignore decimal points if input number less than 57 estimated time will be 0

for example if i enter input miles 280 result is "Estimated driving time 4.977777777777778 hours" i wanted to make something like 4hours 59mins in this case
distanceMiles = input("Miles ? ")
distanceKM = int(distanceMiles) * 1.6
meters = int(distanceKM) * 1000
centimeter = int(meters) *100

drive = distanceKM/90  #speed per hour

print(" in kilometers is", distanceKM, "km")
print(meters, "meters")
print(centimeter, "cm")
print("Estimated driving time",drive, "hours")
Wall
Reply
#2
If you want just hours, you can ask python to limit the number of decimals.
>>> hours = 22/7
>>> print(f"{hours:.2f} hours to drive")
3.14 hours to drive
It would also not be too hard to convert that manually to hours:minutes (remove integer hours, multiply fractional part by 60).

>>> h,m = divmod(hours*60, 60)
>>> m = round(m)
>>> print(f"The drive should take {h:.0f}:{m:02d}")
The drive should take 3:09
datetime/timedelta can do that for you as well, but I find it annoying that the formatting options for timedelta are so limited.

>>> from datetime import timedelta
>>> str(timedelta(hours=hours))
'3:08:34.285714'
You'd have to split out the string as needed, or maybe pull total_seconds() from it and trim to an int to remove the sub-second part....
Reply
#3
2nd option worked well, first option code only gives result 3.14 i think i made something wrong tnx a lot for your answer
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  currency converter using forex-python preethy12ka4 8 731 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  Help with basic weight converter PythonSquizzel 3 1,422 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  Help me get this image converter code working? NeTgHoSt 0 2,090 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Currency converter Scott 5 3,007 Jun-14-2020, 11:59 PM
Last Post: Scott
  How to use 2to3.py converter under Windows OS samsonite 2 7,324 Mar-02-2019, 05:49 AM
Last Post: samsonite
  Can't get a downloaded file converter to work Godotisnothere 1 3,188 Jan-24-2017, 06:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020