Practice Exercises for Timers

Contents

Practice Exercises for Timers#

Solve each of the practice exercises below. Each problem includes three CodeSkulptor links: one for a template that you should use as a starting point for your solution, one to our solution to the exercise, and one to a tool that automatically checks your solution.

1#

The following program should count upward from zero. Print the counter values 0, 1, 2, … to the console. Add two lines of Python to make this program work. Hint: Add a global variable to a timer callback, and start a timer. Counter templateCounter solutionCounter (Checker)

# Counter ticks

###################################################
# Student should add code where relevant to the following.

try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui
 
counter = 0

# Timer handler
def tick():
    global counter
    print(counter)
    counter += 1

# create timer
timer = simplegui.create_timer(1000, tick)
timer.start()

2#

Given the solution from the following problem, we again want a counter printed to the console. Add three buttons that start, stop and reset the counter, respectively. Counter buttons templateCounter buttons solutionCounter buttons (Checker)

3#

Use a timer to toggle the canvas background back and forth between red and blue every 3 seconds. Use the CodeSkulptor Docs to locate the appropriate call to change the background color of the canvas. Toggle background templateToggle background solutionToggle background (Checker)

4#

Create a circle in the center of the canvas. Use a timer to increase its radius one pixel every tenth of a second. Expanding circle templateExpanding circle solutionExpanding circle (Checker)

5#

Challenge: Use a timer to measure how fast you can press a button twice. Create a button that starts a timer that ticks every hundredth of a second. The first button press starts the measurement. The second button press ends the measurement. Print to the console the time elapsed between button presses. The next two button presses should repeat this process, making a new measurement. Hint: We suggest that you keep track of whether the program is on the first or second button press using a global Boolean variable. Reflex test templateReflex test solutionReflex test (Checker)

# Reflex tester

###################################################
# Student should add code where relevant to the following.

try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui
 

total_ticks = 0
first_click = True


# Timer handler
def tick():
    global total_ticks
    if not first_click:
        total_ticks += 1


# Button handler
def click():
    global first_click
    global total_ticks
    if first_click:	
        total_ticks = 0
    else:
        print(total_ticks*10, 'ms')

    first_click = not first_click

# Create frame and timer
frame = simplegui.create_frame("Counter with buttons", 200, 200)
frame.add_button("Click me", click, 200)
timer = simplegui.create_timer(10, tick)

# Start timer
frame.start()
timer.start()
  • alternatively we can timer.start() timer.stop() at whenever click() occurs.

  • My reaction was 130 ms :)