I have a problem regarding the hour and minutes. I have a program in python. Which the user is asked to enter the minutes. And that needs to be converted to hours and minutes. The seconds are not taken into account, that is, the user is not asked to enter them.
I wear this now.
dato= int(input("Ingrese los minutos: "))
hora = dato/60
minutos = (segundos-hora*3600)/60
Usually this is how you get the minutes. But in this case the user will not be prompted for seconds. So I thought I'd remove that and leave it like this:
minutos = (hora*3600)/60
But after testing it clearly doesn't work. Because taking an example. If the user enters, as data. The number 305. That's 5 hours and 5 minutes. With the first calculation it was possible to obtain the hours but not the minutes. I would like to know what I am wrong. Thank you very much.
try this friend
Cheers
First,
hora = dato/60
it gives you the hours in decimal format, for the example you give it would be5.083333333333333
. You are only interested in the integer part, the decimals are the remaining minutes.Since you keep everything,
minutos = (hora*3600)/60
it just reverses the previous operation, so you get 305 again...You can do something like:
The operator
//
is integer division, which returns only the integer part of the division, 5 in your example. Alternatively you can use the modulo operator%
(dato % 60
) to calculate the minutes, instead of calculating the rest "manually".If you can use
divmod(a, b)
, return the integer part and the modulus (remainder) of dividinga
byb
, simply:I would do it like this, but I'll tell you that I've only been using python for a couple of months, I'm sure there's a cleaner way, I don't know if using format in floats...