-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek08.py
More file actions
67 lines (54 loc) · 1.87 KB
/
week08.py
File metadata and controls
67 lines (54 loc) · 1.87 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
65
66
67
# Week 8: Pattern Maker (Loops + Turtle)
# Goal: Use nested loops to create patterns and understand angle mathematics
# New concept: WHILE LOOPS as alternative to for loops
import turtle
# Setup screen
screen = turtle.Screen()
screen.title("Pattern Maker")
# Create turtle
t = turtle.Turtle()
t.speed(0) # 0 = fastest (instant drawing, no animation)
# ===== PART 1: Draw a regular polygon (configurable) =====
# Try changing these values to see different shapes!
sides = 6 # Number of sides (3=triangle, 4=square, 6=hexagon, etc.)
step = 80 # Length of each side
turn = 360 / sides # Calculate angle: 360 degrees / number of sides
# Draw the polygon
for _ in range(sides):
t.forward(step)
t.right(turn)
# ===== PART 2: Create a circular pattern of squares =====
# This demonstrates NESTED LOOPS (loop inside a loop)
# Reset turtle to center
t.penup()
t.goto(0, 0)
t.pendown()
# Outer loop: How many times to repeat the pattern
for _ in range(18):
# Inner loop: Draw one square
for _ in range(4):
t.forward(60)
t.right(90)
# Rotate before drawing next square
t.right(20) # 18 squares × 20 degrees = 360 degrees (full circle)
# ===== EXAMPLE: While loop alternative =====
# Uncomment this section to try while loops instead!
# This creates the same pattern as above, but uses while instead of for
# t.clear() # Clear screen first
# t.penup(); t.goto(0, 0); t.pendown()
#
# rotation = 0
# while rotation < 360: # Keep going WHILE less than 360 degrees
# # Draw one square
# for _ in range(4):
# t.forward(60)
# t.right(90)
#
# # Rotate and update counter
# t.right(20)
# rotation = rotation + 20 # IMPORTANT: Must update or loop runs forever!
#
# print(f"Drew {rotation // 20} squares") # How many did we draw?
# Hide turtle and finish
t.hideturtle()
screen.mainloop()