0

This Is my Program Code where I have created a function that is converting Hour to Min.

    def convertToHM(Mins):
        hours = Mins / 60
        minutes = Mins % 60 
        
        return str(hours) + ':' + str(minutes)
    
Mins = int(input("Enter Minutes: "))
print("OutPut Is " + convertToHM(Mins))

OutPut Of the above Code. Enter Minutes: 63 OutPut Is 1.05:3

My Question Is why it is giving 1.05:3, I want my output as 1:3 where I am making Mistakes?

I even have tried to convert string to integer

return int(str(hours)) + ':' + int(str(minutes))

**Traceback (most recent call last):
  File "<string>", line 14, in <module>
File "<string>", line 10, in convertToHM
ValueError: invalid literal for int() with base 10: '1.05'**

so it is giving me the above error.

How should I overcome this error? I am a very Beginner in Python Language

Daya Yadav
  • 11
  • 1
  • 2

2 Answers2

0

/ is float division, you need integer division //

Also, you can use str.zfill to show the zero padded minutes

def convertToHM(Mins):
    hours = Mins // 60
    minutes = Mins % 60
    return str(hours) + ':' + str(minutes).zfill(2)
Mins = int(input("Enter Minutes: "))
print("OutPut Is " + convertToHM(Mins))
Enter Minutes: >? 63
OutPut Is 1:03

Without zfill, output is 1:3

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
0

You can use the function math.trunc to truncate your number and keep the integer part of it :

    hours = math.trunc(Mins / 60)