import scene3d
scene = scene3d.Scene()
scene.set_sky('#1a1a2e')
scene.set_ground(length=12, width=8)
box1 = scene3d.Shapes.Box(width=1, height=1, depth=1)
box1.set_color('#e94560')
box1.set_position(-3, 0.5, 0)
scene.add(box1)
box2 = scene3d.Shapes.Box(width=1, height=1, depth=1)
box2.set_color('#e94560')
box2.set_position(-1, 0.5, 0)
scene.add(box2)
box3 = scene3d.Shapes.Box(width=1, height=1, depth=1)
box3.set_color('#e94560')
box3.set_position(1, 0.5, 0)
scene.add(box3)
box4 = scene3d.Shapes.Box(width=1, height=1, depth=1)
box4.set_color('#e94560')
box4.set_position(3, 0.5, 0)
scene.add(box4)For Loops and While Loops
What if you wanted to place ten boxes in a row? Or a hundred stars scattered across the sky? Writing the same code over and over would take forever — and one typo ruins everything.
A loop lets you repeat code automatically. Python gives you two kinds:
- A
forloop runs a block of code once for each item in a sequence. - A
whileloop keeps running as long as a condition stays true.
Four Boxes the Hard Way
Run the cell below. Four boxes appear in a row. Each box requires three nearly identical lines of code. Imagine having to do this for twenty boxes!
Four Boxes the Smart Way
A for loop does the same job in just a few lines. Run the cell below — you’ll get the exact same four boxes.
import scene3d
scene = scene3d.Scene()
scene.set_sky('#1a1a2e')
scene.set_ground(length=12, width=8)
for i in range(4):
box = scene3d.Shapes.Box(width=1, height=1, depth=1)
box.set_color('#e94560')
box.set_position(i * 2 - 3, 0.5, 0)
scene.add(box)How the for Loop Works
for i in range(4):
box = scene3d.Shapes.Box(width=1, height=1, depth=1)
box.set_position(i * 2 - 3, 0.5, 0)
...| Part | What it does |
|---|---|
for |
The keyword that starts the loop |
i |
A variable that takes each number from range, one at a time |
range(4) |
Produces the numbers 0, 1, 2, 3 — four values total |
: |
Marks the end of the for line |
| Indented body | The code that repeats — one run per value in range |
On the first pass, i is 0. Second: i is 1. And so on through 3. The expression i * 2 - 3 gives x-positions of -3, -1, 1, 3 — spacing the boxes evenly.
Indentation matters!
Just like with if statements, everything indented under the for line is inside the loop. Return to the left margin and you’re outside again.
{ “question_type”: “multiple_choice”, “question”: “What values does range(4) produce?”, “options”: [ { “key”: “a”, “text”: “1, 2, 3, 4” }, { “key”: “b”, “text”: “0, 1, 2, 3” }, { “key”: “c”, “text”: “0, 1, 2, 3, 4” }, { “key”: “d”, “text”: “4, 3, 2, 1” } ], “answer”: “b”, “submitted_answer”: “” }
{ “question_type”: “true_false”, “question”: “If you write ‘for i in range(3):’, the loop body will run exactly 3 times.”, “answer”: “True”, “submitted_answer”: “” }
Using i to Create Variety
The loop variable i isn’t just a counter — you can use its value to make each object different. The scene below uses i to control both the height and the color of each box, creating a staircase.
import scene3d
scene = scene3d.Scene()
scene.set_sky('#0f3460')
scene.set_ground(length=15, width=8)
colors = ['#e94560', '#f5a623', '#44cc88', '#4488ff', '#cc44ff']
for i in range(5):
box_height = i + 1
box = scene3d.Shapes.Box(width=1.2, height=box_height, depth=1.2)
box.set_color(colors[i])
box.set_position(i * 2.5 - 5, box_height / 2, 0)
scene.add(box){ “question_type”: “multiple_choice”, “question”: “In the staircase example, what is the value of i during the very first pass through the loop?”, “options”: [ { “key”: “a”, “text”: “1” }, { “key”: “b”, “text”: “-1” }, { “key”: “c”, “text”: “0” }, { “key”: “d”, “text”: “5” } ], “answer”: “c”, “submitted_answer”: “” }
while Loops — Repeat Until a Condition Changes
A for loop is great when you know exactly how many times to repeat. But sometimes you want to keep going until something happens — and you don’t know in advance how many steps that takes.
That’s what a while loop is for:
while condition:
# runs as long as condition is TrueThe scene below builds a tower by stacking boxes. The loop keeps adding a box while the current height is less than 5 — and stops the moment that’s no longer true.
Hint: Use the scroll wheel to zoom out and see the full tower.
import scene3d
scene = scene3d.Scene()
scene.set_sky('#87CEEB')
scene.set_ground(length=12, width=12)
height = 0.5
while height < 5:
box = scene3d.Shapes.Box(width=1.5, height=1.0, depth=1.5)
box.set_color('#cc4400')
box.set_position(0, height, 0)
scene.add(box)
height += 1.0How the while Loop Works
height = 0.5
while height < 5:
...
height += 1.0| Step | What happens |
|---|---|
| 1 | Python checks: is height < 5? |
| 2 | If True → run the indented body |
| 3 | After the body, go back to step 1 |
| 4 | If False → exit the loop and continue |
Warning — Infinite Loops! If the condition never becomes False, the loop runs forever and your program hangs. Always make sure something inside the loop changes the condition. Here,
height += 1.0keeps growingheightuntil it reaches 5.
for vs while — which one to use?
Use for when… |
Use while when… |
|---|---|
| You know exactly how many times to repeat | You repeat until a condition changes |
| Stepping through a fixed sequence | The number of repetitions isn’t known upfront |
range(n) fits naturally |
You’re waiting for something to happen |
{ “question_type”: “true_false”, “question”: “A while loop automatically stops after running exactly 10 times, no matter what the condition says.”, “answer”: “False”, “submitted_answer”: “” }
{ “question_type”: “multiple_choice”, “question”: “Which loop is the most natural choice for placing exactly 8 spheres in a scene?”, “options”: [ { “key”: “a”, “text”: “A while loop” }, { “key”: “b”, “text”: “A for loop with range(8)” }, { “key”: “c”, “text”: “Neither — just write 8 separate lines of code” }, { “key”: “d”, “text”: “It doesn’t matter — for and while always do the same thing” } ], “answer”: “b”, “submitted_answer”: “” }
A Note on do/while
Some languages have a do/while loop that always runs the body at least once before checking the condition. Python doesn’t have this keyword, but you can get the same effect using while True: with break:
while True:
# this always runs at least once
count += 1
if count >= 5:
break # exit the loop immediatelybreak exits the loop right away, jumping to the first line after it. while True: would run forever on its own — break is how you decide exactly when to stop.
Try It Yourself
Use the slider to choose how many spheres appear in the scene. The for loop adapts automatically — change the count, rerun the cell, and see the result.
import scene3d
COUNT = 5 #@param {type:"slider", min:1, max:10, step:1}
scene = scene3d.Scene()
scene.set_sky('#0f3460')
scene.set_ground(length=25, width=8)
colors = ['#e94560', '#f5a623', '#4488ff', '#44cc88', '#cc44ff',
'#ff44cc', '#44ffcc', '#ffcc44', '#ff8844', '#88ff44']
spacing = 20 / COUNT
for i in range(COUNT):
sphere = scene3d.Shapes.Sphere(diameter=1.2, segments=16)
sphere.set_color(colors[i % len(colors)])
sphere.set_position(i * spacing - 10 + spacing / 2, 0.7, 0)
scene.add(sphere)Think of a real scene you’d like to build — a forest of trees, a city block of buildings, a night sky full of stars, a row of traffic cones.
Which kind of loop would you use, and why? What code would go inside the loop? Write a short description of your scene and how the loop helps build it.