Chunk d quiz
โ๏ธ Quiz: Understanding loops
Solve the quizzes below to check what you learned! ๐ฏ
If you're not sure, read it again and give it a try! You've got this! ๐ช
Quiz 1: The loop keyword
Hint: Enter the keyword you learned in this lesson.
Show Answer
Answer: for โ
for starts a loop with this keyword! ๐ฌ
for Next:
- The variable name (i) ๐งบ - the basket
- in - the connecting word
- the range to loop over (range(8)) ๐ญ - the number factory
The code inside a loop must be indented! ๐
Quiz 2: The range() function
range(5)which numbers does it make?
Show Answer
Answer: B) 0, 1, 2, 3, 4 โ
range(5)makes 5 numbers from the number factory, starting at 0! ๐ญ
In Python, we start counting from 0! (0-based indexing)
range(n)makes the numbers from 0 to n-1!
range(5)โ 0, 1, 2, 3, 4 (5 numbers)range(8)โ 0, 1, 2, 3, 4, 5, 6, 7 (8 numbers)
Quiz 3: Indentation
Show Answer
Answer: A) It must be indented โ
Indentation is very important in Python! The code inside a loop must always be indented! ๐
# Correct way โ
for i in range(3):
print(i) # ๐ Inside the room, so it repeats
# Wrong way โ
for i in range(3):
print(i) # ๐ช Outside the room, so it errors!
If you don't indent, an error, IndentationErroroccurs! ๐
Remember: Only the code inside the loop's room repeats! ๐
Quiz 4: Number of repetitions
for i in range(8):how many times does it repeat?Hint: Enter a number.
Show Answer
Answer: 8 โ
range(8)makes 8 numbers from the number factory, 0 to 7, so the loop runs 8 times! ๐ญ๐
range(n)repeats exactly n times!
range(3)โ repeats 3 times (0, 1, 2)range(5)โ repeats 5 times (0, 1, 2, 3, 4)range(8)โ repeats 8 times (0, 1, 2, 3, 4, 5, 6, 7)
Next step
Congratulations! ๐ You now understand loops!
You learned how to repeat the same code by spinning like a carousel! ๐
In the next lesson: - You'll learn how to use a list to store many objects ๐ฆ - You'll learn how to use lists and loops together ๐
๐ฎ Try it yourself!
Experiment by changing the number in the number factory! ๐ญ
range(8)the range(10)or range(5)Try changing it to this and see what happens!
When the number of repetitions changes, the number of cubes made changes too!
Editing the code yourself and checking the results is the best way to learn! ๐ช
๐งฉ Reviewing the Object concept
- An object is a LEGO blockLike this, it's an entity that has both properties and behaviors. ๐งฉ
- A variable is sticky note ๐ท๏ธ, and functions are Instruction manual for behaviors, and an object is a toy that combines the two.
- Ursina's Entity is an object too!
- Properties:
model,color,scale(settings decided at birth) ๐จ - Behaviors:
animate_position()(actions it can do later) ๐ฌ - Putting objects into a list gives you a basketit becomes. ๐๏ธ