Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

import threading

import time

class Countdown:

def __init__(self):

self.start_time = 0

self.remaining = 0

self.thread = None

self.active = False

self.paused = False

def start(self, seconds):

if self.active:

return

self.start_time = seconds

self.remaining = seconds

self.active = True

self.paused = False

self.thread = threading.Thread(target=self._run)

self.thread.start()

def _run(self):

while self.remaining > 0 and self.active:

if not self.paused:

print(f"Remaining: {self.remaining} seconds", end="\r")

time.sleep(1)

self.remaining -= 1

if self.remaining == 0:

print("\nDone!")
self.active = False

def pause(self):

if not self.active or self.paused:

return

self.paused = True

def resume(self):

if not self.paused:

return

self.paused = False

def reset(self):

self.active = False

self.paused = False

self.remaining = self.start_time

def stop(self):

if not self.active:

return

self.active = False

self.paused = False

# Ask the user for the countdown duration

duration = int(input("Enter the countdown time in seconds: "))

# Create an instance of the Countdown class and start the countdown with the user's input

countdown = Countdown()

countdown.start(duration)
# To control the countdown dynamically, consider implementing a way to call pause(), resume(), reset(),
and stop() based on user input.

You might also like