Project Introduction
In this tutorial, we’ll be creating a countdown timer that asks the user how much time (in seconds) they want to set the timer for — once time is up, print out “Blast Off!”.
We’ll also be creating an extension to this project where we’ll make a Pomodoro Timer with 25-minute and 5-minute intervals — the Pomodoro technique is a productivity method that is often used while studying or working. It uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short 5 minute breaks.
Watch the tutorial video to see how we code this game step-by-step, and continue reading this post for more details!
This project info and learning outcomes summary will help you decide if this Python coding project is right for you.
This beginner/intermediate Python tutorial is for students that want an Easy challenge project, about 10-15 lines of code for countdown timer, and 20-30 lines of code long for the pomodoro timer. You should review Python loops, strings, inputs, and conditionals beforehand to get the most out of this project.
Some other projects you can try first for more practice with beginner/intermediate Python are our how to make a calculator in Python and draw an animal face in Python tutorials.
For learning outcomes, you'll get a lot of practice with user input, while loops, modulo, Python time module, and floor div. This project is estimated to take you about 15-30 minutes, but you should move faster or slower at your own pace!
Project Demo
Before getting started, see how our finished project works for reference. Watch the video, or click run to see the project yourself!
You can also view my project solution code if you get stuck.
What to keep in mind before you start:
Remember that floor division in Python returns the quotient, but rounds down to the nearest integer. For example, 10 / 3 = 3.333, but 10 // 3 = 3.
The % symbol is called the modulo operator. It returns the remainder of dividing the left hand operand by right hand operand. For example, 10 % 3 = 1, because 3 goes into 10 three times, with a remainder of 1.
Steps to Code the Project:
Set up the project and create a function that takes in an the number of seconds we want to count down for.
Use a while loop so that we keep counting down until t is 0.
Calculate the amount of time remaining and print out the timer.
How do we do each of these steps?
Step 1: Set up the project and create a function that takes in the number of seconds we want to count down for.
Import the time module and ask the user for the number of seconds they want to set the timer for.
Step 2: Use a while loop so that we keep counting down until t is 0.
Inside the loop, calculate the amount of time in minutes and seconds that the timer has left. (Hint: // and % may be helpful here. Remember that each minute has 60 seconds!)
Once you calculate the number of minutes and seconds, format the time to be in minutes:seconds using timer = '{:02d}:{:02d}'.format(mins, secs).
Step 3: Calculate the amount of time remaining and print out the timer.
Next, print out the timer and make sure to overwrite the previous time as the amount of time remaining continuously updates. (Hint: Use print(timer, end="\r")), the \r will allow us to overwrite the previous line).
Be sure to call time.sleep(1) to make sure our timer waits one second before decreasing as well as decrement it by 1 each time.
Once the timer runs out, print “Blast Off!”
Bonus: Make a Pomodoro Timer
Transform your countdown timer into a Pomodoro timer! It uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short 5 minute breaks.
Step 1. Set up the project and create a function.
Import the time module and print out “Pomodoro starts now!” at the beginning outside of the function
Step 2. Use a loop to create the 25-minute work time intervals. Traditionally, the Pomodoro technique has four intervals of 25-minute work time followed by 5-minute break time. We can use a for loop to repeat the following code four times.
Create a variable to keep track of the time remaining. Remember that each minute has 60 seconds, so our 25-minute work time should have 25*60 seconds at the start.
Calculate the amount of time in minutes and seconds that the timer has left. (Hint: // and % may be helpful here.)
Similar to the countdown timer from above, format the time to be in minutes:seconds using timer = '{:02d}:{:02d}'.format(mins, secs)
Next, print out the timer and make sure to overwrite the previous time as the amount of time remaining continuously updates. (Hint: Use print(timer, end="\r"), the \r will allow us to overwrite the previous line) .
After the 25-minute work time is up, print out “Break Time!
Step 3. Create the 5-minute break time timer.
Repeat Step 2 to program a 5-minute break time timer. Once the time is up, print out “Work Time!”
That's it! Great job!
Check out Juni Learning's step-by-step coding projects to find their other tutorials in Python and more coding languages!
A Juni Instructor teaches basic Python to a young student.
This article originally appeared on junilearning.com.
Comments