-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmoney.py
More file actions
51 lines (41 loc) · 1.25 KB
/
money.py
File metadata and controls
51 lines (41 loc) · 1.25 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
# interactive application to convert a float into dollars and cents
import simplegui
# define global value
value = 3.12
# 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 draw handler
def draw(canvas):
canvas.draw_text(convert(value), [60, 110], 24, "White")
# define an input field handler
def input_handler(text):
global value
value = float(text)
# create frame
frame = simplegui.create_frame("Converter", 400, 200)
frame.add_input("Enter value", input_handler, 100)
# register event handlers
frame.set_draw_handler(draw)
# start frame
frame.start()