-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2-p2.py
70 lines (55 loc) · 1.45 KB
/
project2-p2.py
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
import turtle
SINGLE_LENGTH = 5
ENCODINGS = [[1, 1, 0, 0, 0], # encoding for '0'
[0, 0, 0, 1, 1], # encoding for '1'
[0, 0, 1, 0, 1], # encoding for '2'
[0, 0, 1, 1, 0], # encoding for '3'
[0, 1, 0, 0, 1], # encoding for '4'
[0, 1, 0, 1, 0], # encoding for '5'
[0, 1, 1, 0, 0], # encoding for '6'
[1, 0, 0, 0, 1], # encoding for '7'
[1, 0, 0, 1, 0], # encoding for '8'
[1, 0, 1, 0, 0] # encoding for '9'
]
def line(tut, digit):
tut.left(90)
if digit == 0:
length = SINGLE_LENGTH
else:
length = 2 * SINGLE_LENGTH
tut.forward(length)
tut.up()
tut.backward(length)
tut.right(90)
tut.forward(10)
tut.down()
def draw_barcode(tut, zip):
line(tut, 1)
for str_digit in str(zip):
digit = int(str_digit)
for bar_bit in ENCODINGS[digit]:
line(tut, bar_bit)
line(tut, 1)
def main():
tut = turtle.Turtle()
tut.speed(0)
tut.pensize(3)
tut.hideturtle()
tut.penup()
tut.setposition(-300, 0)
tut.pendown()
# 55555-1237
draw_barcode(tut, 555551237)
tut.penup()
tut.setposition(-300, -50)
tut.pendown()
# 91768-1234
draw_barcode(tut, 917681234)
tut.penup()
tut.setposition(-300, -100)
tut.pendown()
# 20500-0000
draw_barcode(tut, 205000000)
tut.getscreen().mainloop()
if __name__ == "__main__":
main()