0

Trying to simulate a countdown without using datetime to decrement from 3 hour 0 mins 0 seconds to zero. Not sure how to set it up as a loop with modulation.

092374
  • 15
  • 1
  • 6
  • 1
    Hint: You may want to use sleep func –  Dec 15 '21 at 18:55
  • 1
    Use a loop with the number of seconds, and do a tiny bit of math to convert it to hours and minutes. – Mark Ransom Dec 15 '21 at 18:56
  • 3
    Why do you *not* want to use `datetime`? Otherwise, you are going to have to hand-code the parsing of your date, then hand-code the subtraction of your dates. Have you tried *anything at all*? – juanpa.arrivillaga Dec 15 '21 at 18:59
  • 1
    First, write a function that accepts integer seconds and prints your string above. Then, it becomes a simple loop, `seconds = 5400` / `while seconds:` / `printsecs(seconds)` / `seconds -= 1` / `time.sleep(1)`. – Tim Roberts Dec 15 '21 at 19:00
  • its for a project prof said specifically not to use datetime – 092374 Dec 15 '21 at 19:00
  • just looking for hints. I know how to do it with datetime not sure how to approach without using datetime – 092374 Dec 15 '21 at 19:05
  • Decrement seconds by 1. If the result is negative, set seconds to 59 and decrement hours by 1. If that result is negative then set minutes to 59 and decrement hours by 1 and if that goes negative then you're done – DarkKnight Dec 15 '21 at 19:25
  • 1
    Does this answer your question? [Countdown Clock: 01:05](https://stackoverflow.com/questions/25189554/countdown-clock-0105) or maybe https://stackoverflow.com/questions/68283946/python-count-down-timer-for-date-hour-minute-and-second – JonSG Dec 15 '21 at 19:34
  • `datetime` is lacking in facilities for handling time *intervals*. While it has `timedelta`, that type is lacking a `strftime`-like method for producing the strings OP wants. – chepner Dec 15 '21 at 19:39
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/334822. – Karl Knechtel Dec 16 '21 at 03:03

2 Answers2

0

I think i figured it out thank you guys.

for x in range(5400,0,-1):
    def format_time(time):
        seconds_to_minute = 60
        seconds_to_hour = 60*seconds_to_minute

        hours = time // seconds_to_hour
        time %= seconds_to_hour

        minutes = time // seconds_to_minute
        time %= seconds_to_minute

        seconds = time
        
        print("%d hours, %d minutes, %d seconds" %(hours, minutes, seconds))

    format_time(x)
092374
  • 15
  • 1
  • 6
  • Putting `def format_time` inside the loop is counter-productive. It works, but you keep re-defining the function unnecessarily. Once is enough. – Mark Ransom Dec 20 '21 at 04:31
-1

You don't need modulation (I assume you mean utilising the modulo operator) or any imported modules. It's as simple as this:

def hms(h, m, s):
    while h >= 0:
        yield h, m, s
        if (s := s - 1) < 0:
            s = 59
            if (m := m - 1) < 0:
                m = 59
                h -= 1

for h, m, s in hms(1, 30, 0):
    print(f'{h} hr {m} min {s} sec')
DarkKnight
  • 19,739
  • 3
  • 6
  • 22