-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoney_converter.py
More file actions
56 lines (40 loc) · 1.16 KB
/
Copy pathmoney_converter.py
File metadata and controls
56 lines (40 loc) · 1.16 KB
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
import simplegui
# define global values
value = 3.12
# helper functions
# Handle single quantity
def convert_units(val, name):
result = str(val) + " " + name
if val > 1:
result = result + "s"
return result
# convert xx.yy to xx dollars and yy cents
def convert(val):
# Split into dollars and cents
dollars = int(val)
cents = int(round(100 * (val - dollars)))
# Convert to strings
dollars_string = convert_units(dollars, "dollar")
cents_string = convert_units(cents, "cent")
# return composite string
if dollars == 0 and cents == 0:
return "Broke!"
elif dollars == 0:
return cents_string
elif cents == 0:
return dollars_string
else:
return dollars_string + " and " + cents_string
# define event handlers
def draw(canvas):
canvas.draw_text(convert(value), [100, 100], 24, "White")
def input_handler(text):
global value
value = float(text)
# create frame
frame = simplegui.create_frame("converter", 400, 200)
# register event handlers
frame.set_draw_handler(draw)
frame.add_input("Enter value", input_handler, 100)
# start frame
frame.start()