Looking for an idea to practise your coding during the holidays? Here's a fun one you can try. Make a clock and a 'tell the time' quiz. For those who need help learning to read the time, this project will also double up as a great tool to use to learn to read the time.
Start by having a look at the video below which will show you what the various stages of this project look like. There are three versions of this project, with varying levels of difficulty. You can choose to do one of them, or even combine two or three into a larger project.
The video will also talk about how to get started with this project and point you in the right direction.
First, you need to create the clock. In our version we have a coloured background and then two dots, one to show the clock face and another to show the rim of the clock where some of the numbers are shown.
This part of the project is not too difficult to write, but may need a few lines of code. Remember to write DRY code (DRY = Don't Repeat Yourself), for example by using for loops to create things that repeat themselves, like the marks around the clock to show the minutes.
Each hand of the clock should have its own turtle to draw it. Remember to use clear and descriptive names to name each turtle. For example fred or peppa_pig are not great names for the turtle drawing the hour hand. Maybe you want to use, let's think, hour_hand as the name for this turtle!
Set the Time on the Clock
(Difficulty: Not too hard)
The easiest of the projects, or stages if you want to code more than one of the suggestions shown in the video above, is the one where you ask the user to type in the hour and minutes and then you draw the clock showing the correct time.
To ask the user to input time, you can either use Python's built-in function input, or turtle's own .textinput or .numinput that can be used on a Screen() that you have created and named. Experiment with these to find out how they work. Only .numinput gives back something that Python will recognise as a number. The other two will give back strings that need to be converted into numbers.
You can either ask for the hours and minutes separately, which is the easier option, or you can ask the user to input the time in a form such as 10:23. In this case, a useful tool you may need is the .split() method that works on strings. Try print("10:23".split(":")) to see what happens.
To show the time, you will need to draw the hour and minute hands. You'll need some arithmetic to get the angles right. Experiment with these. A useful command in the turtle module is .setheading() that works on a Turtle(). This allows you to change the heading of a turtle to any direction you wish. When a turtle points to the right, the angle is 0º. To make the turtle face upwards, you'll need hour_hand.setposition(90), for example, for a turtle named hour_hand.
Make a "Tell The Time" Quiz
(Difficulty: A bit harder)
If you want to add more features to your program, you may want to start thinking about defining functions. For example, the code that draws the hour and minute hands based on the time will be needed many times, so you may want to define a function, for example:
def draw_hands(hour, minute): # Write your code here
You can then write another function to run a quiz in which a random hour and minute are chosen and drawn, and the player needs to write the correct time. This part is similar to what you had to do in the Set the Time on the Clock section. You can choose how many questions to ask, and then provide the score at the end.
Show a Live Clock
(Difficulty: Hmm, a bit tricker still)
You can now draw a clock showing the correct time either based on what the user of the program typed in, or based on an hour and minutes chosen randomly. However your computer knows the time, and you can read this using Python. So you can now create a live clock that displays the current time continuously.
There is a module you can import called datetime and then you can get the current time as follows:
time_now = datetime.datetime.now()
Try to see what happens if you try the following:
You have now been able to get the current time in your Python program. You should therefore be able to draw the clock showing the correct time.
But how to make the clock keep displaying the time continuously? How do we make something keep happening forever in Python?
One bit of advise. You probably don't want your clock to update several hundred times a second. That will make you program work very hard but you won't see any difference. You can ask your program to pause for a second to give it a break when you need to:
import time time.sleep(1)
That's right, your program will have a micro-nap for one second!
If you're showing a live clock, you may also want to make sure to use the .tracer(0) and .update() commands on the turtle.Screen() to make you drawings appear instantaneously. Remember how to use this. (Hint: the .update() needs to be inside any loop you write to draw the clock continuously).
Here's my version of the live clock (although this is a video, so it's very unlikely it will show the correct time for you, unless you happen to run the video at exactly the right time of the day!)