E. Loops and Lists
🎯 Learning Goals
By the end of this lesson, you'll be able to:
- ○ Be able to use a list together with a loop
- ○
len()function to check the length of a list - ○ Be able to process all bullets/enemies in a game
Core Concepts
Iterating over a list
🔄 Analogy: standing in line
Just like People standing in lineLike checking them one person at a time! It processes every item in the list one by one.
The len() function
len() function returns the number of items in a list.
Using it in games
# Move all bullets
for bullet in bullets:
bullet.position += bullet.forward * speed
# Check whether any enemies remain
if len(enemies) == 0:
print("Victory!")
Example Code
from ursina import *
app = Ursina()
bullets = []
targets = []
# Create targets
for i in range(6):
target = Entity(
model='cube',
color=color.white,
position=(i*2 - 5, 0, 5)
)
targets.append(target)
def update():
# Move all bullets
for bullet in bullets:
bullet.z += 0.5 # Move forward
# Check for victory
if len(targets) == 0:
print("All targets removed!")
EditorCamera()
app.run()
What Happens
All bullets are processed with a loop!
List function summary
| Function | Role | Example |
|---|---|---|
append() |
Add | my_list.append(item) |
remove() |
remove | my_list.remove(item) |
len() |
Count | len(my_list) |
Quiz
Show Answer
Answer: C) len()
len() is short for "length."
It returns the number of items in a list, string, and so on!
Loop and list practice:
fruits = ["사과", "바나나", "오렌지"] Given a list:
- To process every item one by one:
for fruitfruits: - To check the number of items in the list (3):
(fruits)→ 3 - Check whether the list is empty:
if len(fruits) ==:
Try Coding It Yourself
Take what you learned with the blocks and write it yourself in Python code!
Iterating over a list
Print every item in the list with a for loop.
Example Answer
Result: 사과, 바나나, and 오렌지 are each printed on their own line!
Checking the count with len()
Use the len() function to check the number of items in a list!
Congratulations! 🎉
You've mastered lists!
Things you can do now:
- ✅ Creating an empty list
[] - ✅ Adding items
append() - ✅ Removing items
remove() - ✅ Checking the count
len() - ✅ Using it together with a loop
Challenge
Challenge 1: ⭐ Easy
for loop to add up all the numbers in the list and print the total.
Hint: total = 0 to start, then add each number.
Challenge 2: ⭐ Easy
Add 5 items to the list and len() to check the count.
Hint: append() 5 times.
Challenge 3: ⭐⭐ Medium
Print each item in the list next to its index number.
Hint: enumerate() function, or range(len(list)) use it.
Challenge 4: ⭐⭐ Medium
Combine two lists into one.
Hint: + operator lets you join lists together.
Challenge 5: ⭐⭐⭐ Hard
Move only the items at or above a certain value into a new list.
Hint: if An if statement and append() use them together.
Challenge 6: ⭐⭐⭐ Hard - Shooting game
- On a mouse click, add a bullet
bulletsto the list update()move all bullets- When a bullet collides with a target, remove both
- Print "Victory!" when all targets are removed
Hint: distance_to() method lets you detect collisions.
🧩 Key Takeaways
What you learned in this lesson:
| Concept | Description |
|---|---|
| for item in list | Iterate over every item in a list |
| len() | Check the number of items |
| Usage | Process all bullets/enemies |
| Victory condition | len(enemies) == 0 |
Review Checklist
- [ ] Do you know how to iterate over a list with a loop?
- [ ]
len()Can you explain the role of the function? - [ ] Do you understand how lists and loops are used in games?