RE: LeoThread 2025-04-25 13:23

You are viewing a single comment's thread:

#askleo Write me a Python code for a countdown program that prints a countdown sequence leading to "Liftoff!"



0
0
0.000
1 comments
avatar

Countdown Program in Python

Here's a simple Python code for a countdown program that prints a countdown sequence leading to "Liftoff!":

import time

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t -= 1
    print('Liftoff!')

# Input time in seconds
t = 10

countdown(t)

How it Works

  • The countdown function takes an integer t as input, representing the time in seconds.
  • The function uses a while loop to decrement the time every second, printing the remaining time in mm:ss format.
  • Once the time reaches 0, it prints "Liftoff!".

Example Use Case

  • Set t to 10 seconds and run the program. The output will be a countdown from 00:10 to 00:00, followed by "Liftoff!".

You can adjust the value of t to change the countdown duration.

0
0
0.000