Python Forum

Full Version: Logic of using floor division and modulus for a different variable at different time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

In the below code(Code_1), please help me understand the logic of dividing minutes and seconds by 10. The second program(Code_2) also gives the same result. But the below program's logic, I'm unable to understand.

Code_1
seconds=eval(input("Please enter the number of seconds: "))
hours=seconds//3600
seconds=seconds%3600
minutes=seconds//60
seconds=seconds%60
print(hours, ":", sep="", end="")
tens=minutes//10
ones=minutes%10
print(tens, ones, ":", sep="", end="")
tens=seconds//10
ones=seconds%10
print(tens, ones, sep="")

Code_2
seconds=eval(input("Please enter the number of seconds: "))
hours=seconds//3600
seconds=seconds%3600
minutes=seconds//60
seconds=seconds%60
print(hours,":",minutes,":",seconds)
// operator is a floor division, so it will just keep the whole number part of the result and discard decimal part.
% is modulus operator, which returns a remainder when you divide two integers. e.g. 7 % 2 -> 1 ( 7 / 2 = 3, 1 remains to 7).
Knowing this, the code is quite easy to understand. I recommend you to take pen and paper, make up a number you will use for initial "seconds" parameter, and do the calculations line by line (of course you can execute single lines in python console too).
Hi,
I did understand why he has used 3600 and 60 in Code_1 as well as Code_2. I didn't understand when he didn't end the program after these steps, why did he extend it by dividing with 10 and considering 10's and 1's places?

Thanks!