-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathas2_picologo.py
More file actions
65 lines (55 loc) · 1.32 KB
/
as2_picologo.py
File metadata and controls
65 lines (55 loc) · 1.32 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
57
58
59
60
61
62
63
64
"""
b – moves the turtle backward 1 unit
d – places the turtle's pen down
f – moves the turtle forward 1 unit
l – turns the turtle to the left by 1 degree
r – turns the turtle to the right by 1 degree
u – raises the turtle's pen up
x – exits the Picologo program
program run: spy3 as2.py < testfile.pico
"""
import turtle
import sys
def draw(n, c):
for i in range(1, n+1):
if c == 'b':
turtle.back(1)
elif c == 'd':
turtle.pendown()
elif c == 'f':
turtle.forward(1)
elif c == 'l':
turtle.left(1)
elif c == 'r':
turtle.right(1)
elif c == 'u':
turtle.penup()
cmd = ['b', 'd', 'f', 'l', 'r', 'u', 'x']
p = []
tmp = sys.stdin.readlines()
for i in tmp:
p.append(i.strip("\n"))
num = 1
line = 0
err = 0
for i in p:
line = line + 1
if len(i)>1:
print("*** command too long at line", line)
err = err + 1
elif i == "":
pass
elif i.isdigit():
num = num * int(i)
else:
if i not in cmd:
print("*** unknown command at line", line)
err = err + 1
continue
if i == 'x':
break
else:
draw(num, i)
num = 1
if err > 0:
print("*** there were %s errors" % str(err))