Skip to content

Commit c0f00e0

Browse files
committed
Reading & processing multi-line records
1 parent 17cc0b5 commit c0f00e0

4 files changed

+120
-2
lines changed

chap01_python101/turtleee.py renamed to chap01_python101/readfile_linebyline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import turtle
22

3-
FILENAME: str = "./src/turtle_commands.txt"
3+
FILENAME: str = "./src/turtle_commands_inoneline.txt"
44

55

66
def main():
@@ -45,7 +45,7 @@ def main():
4545
else:
4646
print(f"Unknown command found in file: {command}")
4747

48-
t.ht()
48+
t.hideturtle()
4949

5050
screen.exitonclick()
5151
print("Program Execution Completed.")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import turtle
2+
3+
FILENAME: str = "./src/turtle_commands_multiplelines.txt"
4+
5+
6+
def main():
7+
t = turtle.Turtle()
8+
screen = t.getscreen()
9+
10+
with open(file=FILENAME, mode="r") as file:
11+
command = file.readline().strip()
12+
13+
while command != "":
14+
if command == "goto":
15+
x, y = float(file.readline()), float(file.readline())
16+
width = float(file.readline())
17+
color = file.readline().strip()
18+
t.width(width=width)
19+
t.pencolor(color)
20+
t.goto(x, y)
21+
elif command == "circle":
22+
radius, width = float(file.readline()), float(file.readline())
23+
color = file.readline().strip()
24+
t.width(width=width)
25+
t.pencolor(color)
26+
t.circle(radius=radius)
27+
elif command == "beginfill":
28+
color = file.readline().strip()
29+
t.begin_fill()
30+
elif command == "endfill":
31+
t.end_fill()
32+
elif command == "penup":
33+
t.penup()
34+
elif command == "pendown":
35+
t.pendown()
36+
else:
37+
print(f"Unknown command found in file: {command}")
38+
39+
command = file.readline().strip()
40+
41+
t.hideturtle()
42+
screen.exitonclick()
43+
print("Program execution completed.")
44+
45+
46+
if __name__ == "__main__":
47+
main()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
beginfill
2+
black
3+
circle
4+
20
5+
1
6+
black
7+
endfill
8+
penup
9+
goto
10+
120
11+
0
12+
1
13+
black
14+
pendown
15+
beginfill
16+
black
17+
circle
18+
20
19+
1
20+
black
21+
endfill
22+
penup
23+
goto
24+
150
25+
40
26+
1
27+
black
28+
pendown
29+
beginfill
30+
yellow
31+
goto
32+
-30
33+
40
34+
1
35+
black
36+
goto
37+
-30
38+
70
39+
1
40+
black
41+
goto
42+
60
43+
70
44+
1
45+
black
46+
goto
47+
60
48+
100
49+
1
50+
black
51+
goto
52+
90
53+
100
54+
1
55+
black
56+
goto
57+
115
58+
70
59+
1
60+
black
61+
goto
62+
150
63+
70
64+
1
65+
black
66+
goto
67+
150
68+
40
69+
1
70+
black
71+
endfill

0 commit comments

Comments
 (0)