In my basketball sim, the quarter clock is set to a 3 digit number, 12 minute quarters * 60 seconds = 720 seconds. After the result of my play, I subtract a random number, in-between 10-24, from the quarter clock. I print the result of my play, as well as the quarter clock. Example code:
quarter_clock = 12 * 60 time_runoff = random.randint(10,24) def play(): if player_makes_shot: print("player makes the shot") quarter_clock = quarter_clock - time_runoff print(quarter_clock)
output:
player makes shot 702
How can I make the clock output to be in a minute-second format like this:
11:42
Thanks for the help! :)
Advertisement
Answer
You can use divmod
, which “returns the quotient and remainder after a division of two numbers”:
>>> divmod(702,60) (11, 42)
So you can do something like this:
>>> minutes, seconds = divmod(702,60) >>> print(f"{minutes}:{seconds}") 11:42
EDIT:
You can also add a 0 to the left of the seconds in case it is a 1-digit number, e.g.:
>>> minutes, seconds = divmod(662,60) >>> print(f"{minutes}:{seconds:02d}") 11:02