Last active
August 29, 2015 14:00
-
-
Save allenaven/11068295 to your computer and use it in GitHub Desktop.
Coursera IIPP Mini-project number 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simplegui | |
my_number = int(0) | |
n_stops = int(0) | |
n_stops_on_zero = int(0) | |
is_running = False | |
def timer_handler(): | |
global my_number | |
my_number += 1 | |
print my_number | |
def draw_handler(canvas): | |
canvas.draw_text(format(my_number), (20, 20), 20, 'White') | |
canvas.draw_text(str(n_stops_on_zero) + '/' + str(n_stops), (100, 20), 20, 'White') | |
def start_handler(): | |
global is_running | |
is_running = True | |
timer.start() | |
def stop_handler(): | |
global n_stops | |
global is_running | |
if is_running: | |
n_stops += 1 | |
timer.stop() | |
is_running = False | |
if my_number % 10 == 0: | |
global n_stops_on_zero | |
n_stops_on_zero += 1 | |
def reset_handler(): | |
global my_number | |
global n_stops | |
global n_stops_on_zero | |
my_number = 0 | |
n_stops = int(0) | |
n_stops_on_zero = int(0) | |
def format(t): | |
"""From t tenths of a second, return time in 0:00.0 format as a string""" | |
A, B, C, D = 0, 0, 0, 0 | |
t = t / 10.0 # Puts t into seconds, not tenths | |
if t > 59: # Figure the minutes | |
A = int(t // 60) | |
t = t - (A * 60) # Subtract, now t is in seconds | |
t = str(t) # Change t to a string | |
t = t.replace('.', '') # And get rid of the decimal in it | |
l = len(t) | |
D = t[l-1] # The last element in t is the tenths | |
C = t[l - 2] # Second to last is the ones | |
if l == 3: # And third to last, if it exists, is the tens | |
B = t[l - 3] | |
return str(A) + ':' + str(B) + str(C) + '.' + str(int(D)) | |
timer = simplegui.create_timer(100, timer_handler) | |
frame = simplegui.create_frame('Stopwatch', 150, 150) | |
start_button = frame.add_button('Start', start_handler) | |
stop_button = frame.add_button('Stop', stop_handler) | |
reset_button = frame.add_button('Reset', reset_handler) | |
frame.set_draw_handler(draw_handler) | |
frame.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment