Greetings guys,
It’s been a very busy week for me, as most of you know I recently reported back to school so I’ve been quite busy with assignments, quizzes and school stuffs. Nevertheless I’m here to share another edition of my Python programming class. For Todays edition, I will try to write a a few lines of code to do a count down after the user have entered a specified number of seconds. I decided to add a Beep sound to spice it up a bit. Let me take you through on how it’s done.
We begin our code by importing a pre defined function from the Python library. And because we want to write a code for a countdown timer, we will need to import the Time function. For the Beep sound to work, I need to also import the winsound function.
Next up is to define a quick few functions before we proceed to the main task which is the countdown timer. The functions we will defining will help in developing the Beep sound later on in the code. The two functions needed are Frequency and Duration. I set the parameters (32767, 1000) respectively.
Now we can now define the functions for the timer. We start by def countdown(seconds) after which we will use the while loop such that the count down is done to a certain number.
Now we need to print the format of the timer such that it starts counting in seconds, the time it takes before it counts the next number and how it counts wether starting from 1’s or 10’s. For this we need three functions, Print(seconds, end=‘…’) for printing the numbers, time.sleep to determine how fast the timer is and lastly seconds to determine the mode on how it counts wethere from 1’s or 10’s
Now we can now go ahead to ask the user to input his choice of seconds to countdown from. So we need to define Seconds input(“Enter the number of seconds for the countdown “). And lastly let’s add the Beep sound. Remember we already defined its functions so it’s just going to be simple here all we need is to call the function winsound.Beep(frequency, duration). I decided to add Print(“Time is up”) to make it look more attractive and nice.
Finally it’s time to run the code and see the results, when we run the code it will request for us to enter the number of seconds to countdown see image below
So I entered 10 and the code returned;
It did the countdown from 10 to 0 and then Beeped followed by a “Time Is up”. Which means our code is running. I’ll leave it here for now and I hope to hear from you on what you think about the code. See your later and Thank you.