Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Convert emoji into text in Python
Next article icon

In this article, we will see how to create a countdown timer using Python. The code will take input from the user regarding the length of the countdown in seconds. After that, a countdown will begin on the screen of the format 'minutes: seconds'. We will use the time module here.

Step-by-Step Approach

In this project, we will be using the time module and its sleep() function. Follow the below steps to create a countdown timer:

1. Import the time module using import time.

2.Get user input for countdown duration (in seconds).

3. Convert input to integer (as input() returns a string).

4. Define a function countdown(t) to perform the countdown.

5. Use a while loop to run the countdown until t reaches 0.

6. Inside the loop:

7. After the loop finishes, print "Fire in the hole!!" to indicate the timer has ended.

Python Code: Countdown Timer

Python
importtimedefcountdown(t):whilet:mins,secs=divmod(t,60)timer='{:02d}:{:02d}'.format(mins,secs)print(timer,end='\r')# Overwrite the line each secondtime.sleep(1)t-=1print("Fire in the hole!!")t=input("Enter the time in seconds: ")countdown(int(t))

Output: 


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp