An Alarm Clock

Introduction to Python Programming for Elementary School Students (Midterm Project)

@ Mr. Eric | Saturday, Oct 3, 2020 | 5 minute read | Update at Saturday, Oct 3, 2020

Many personal computers, laptop computers, tablets, and smart phones have applications that allow a user to set reminder alarms. To accomplish this, all these applications use date and time functions that are built into the language they are programmed in.

Midterm project requirements:

In this project, you’ll create an alarm clock program that shows an announcement message at a user-inputted time and then draws an image of a clock face using the turtle module. You must incorporate the following:

  1. (20 points) The program shall ask the user for the time that the alarm should display the announcement message and the message that should be displayed.
  2. (20 points) The program shall use the datetime module for Python to get the current time.
  3. (10 points) The program shall monitor the time and display the message at the given time.
  4. (20 points) The program shall display a drawn clock face using the turtle module as part of the announcement that the alarm has finished. The clock face may display any time desired or simply be a drawn clock.
  5. (15 points) The program shall be commented to explain the programmer’s thought process.
  6. (15 points) The program shall be designed for usability, readability, and consistency.

Midterm project instructions:

This project will require you to integrate several of the concepts you’ve learned so far in this course. Taking a careful read of the instructions, let’s break down what should happen in this program.

  1. Ask the user for the time that the alarm should be displayed. Let’s think about how to do that. To make it easy using the datetime module, we should ask the user for the hour and the minute of the desired time separately. Then, we need to ask the user for the message that should be displayed when the alarm time is reached.
  2. Ask the user for the message that should be displayed when the alarm time is reached. Now we’re on to the tough part! How do we compare the time that the user gave us to the current time? That’s when you need to use the datetime module. We’re going to have to use two loops here: one when we check the hour and one when we check the minute, as well as some if statements!
  3. Check the current hour and see if it’s the same as the hour requested.
    1. If it’s the same, check the current minute and see if it’s the same as the minute requested.
      1. If it’s the same, display the message and draw the clock picture.
      2. If it’s not the same, continue checking the current minute until it is.
    2. If it’s not the same, continue checking the current hour until it is.

Now that you’ve designed your program, let’s think about testing it. Since it will have to keep running until the alarm time is reached, try entering the time one or two minutes from now and letting it run to see if it works! You can design your clock face by itself and then add it to the appropriate part of the alarm once it’s working. That way, you don’t have to sit and wait for the right time to be reached each time you want to test your program!

My Answer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Importing datetime and turtle...
import time
import datetime
import turtle
dot = 1
# Getting user's alarm-clock hour
print('In which hour do you want to be alarmed(00, 01, 02, 03...21, 22, 23)?')
hour = int(input())
# Getting user's alarm-clock minute
print('In which minute do you want to be alarmed(00 ~ 59)?')
minute = int(input())
# Gettine user's message
print('What messge do you want to display?')
message = input()
not_time = True
# Nesting...
while (not_time):
    if int(datetime.datetime.today().strftime("%H")) == hour and int(datetime.datetime.today().strftime("%M")) == minute:
        print('Your message: ' + message)
        break
    else:
        print('Keep waiting please' + dot * '.')
        dot += 1
        if dot == 61:
            dot -= 1
            while dot < 61 and dot > 1:
                dot -= 1
                print('Keep waiting please' + dot * '.')
        elif dot == 1:
            dot += 1
            while dot > 1 and dot < 61:
                dot += 1
# Finished!
t = turtle.Pen() # Setting up turtle.
t.speed(10) # Making a faster turtle.
t.penup()
t.goto(0, -230) # All turtles at your position!
t.pendown()
def draw(): # Defining a draw function to draw the lines.
    t.left(90)
    t.forward(40)
    t.backward(40)
    t.right(90)
for i in range(12): # Now drawing...
    draw()
    for x in range(30):
        t.forward(4)
        t.left(1)

t.left(90) # You know that all clocks have a midpoint...
t.penup() # I can't draw while moving
t.goto(5, -5)
t.pendown()
t.begin_fill() # The midpoint shall be like black and filled.
t.circle(5)
t.end_fill() # Can't fill anymore. The midpoint's done.
t.penup()
t.goto(0, 0)
t.pendown() # Turtle is ready ro draw the indecators
t.right(30 * hour + 0.5 * minute)
t.color('red')
t.forward(100)
t.clone()
t.backward(100)
t.setheading(90)
t.color('blue')
t.right(6 * minute)
t.forward(150)

Result

Result

Code
Save as image
Mr. Eric
Life writes the best stories. Let’s study together! Never copy please!
A Friendly Introduction to Number Theory Algebra Ii Algebra Problem Algorithm Code Euclid Geometry Germination Method Number Theory Opinion Poetry Appreciation Reflection Seed Solve Speech Stop Motion

© 2020 - 2023 Mr. Eric

Powered by Hugo with theme Dream.

Mr. Eric

I am a student, learn, happy.

Always happy.