Practice and reinforce the concepts from Lesson 6
for
loops to iterate through listswhile
loops with conditionselse
statements with loopsbreak
and continue
:information_source: Time Required Total activity time: 45-60 minutes
- Exercise Set 1: 25-30 minutes
- Exercise Set 2: 20-30 minutes
Time estimate: 25-30 minutes
Write the program using loops to show the outcome for each question.
For and While loop comparison
Using for loop:
one_to_ten
Using while loop:
:bulb: :bulb: Hint: For the while loop, don't forget to:
- Start your counter at 1
- Use the condition
counter <= 10
- Increment the counter inside the loop
Expected output:
1
2
3
4
5
6
7
8
9
10
Using else statement with loops
multiply5
else
to print "That's all for the items in the list" after the loop completesExpected output:
5
10
15
20
That's all for the items in the list
else
to print "That's all multiples of 7 up to 70" when done:bulb: :bulb: Remember: The
else
block executes when the loop completes normally (not interrupted bybreak
)
Expected output:
text7 14 21 28 35 42 49 56 63 70 That's all multiples of 7 up to 70
Question 3: Break Statement ⏱️ 5-7 minutes
Controlling loop execution with break
- For loop with break:
- Use
range(11, 21)
to generate numbers 11 to 20- Print each number
- Use
break
when the number equals 15- While loop with break:
- Start with counter at 11
- Print numbers but stop when reaching 15
- Use
break
to exit the loop tip :bulb: Note: Thebreak
statement should be inside anif
condition checking if the current number is 15
Expected output:
11
12
13
14
15
Skipping iterations with continue
For loop with continue:
range(11, 21)
for numbers 11 to 20continue
to skip to the next iterationWhile loop with continue:
continue
:bulb: :warning: Common mistake: In the while loop, make sure to increment your counter BEFORE the
continue
statement, or you'll create an infinite loop!
Expected output:
text11 12 13 14 16 17 18 19 20
:art: Exercise Set 2: Real-World Application
Time estimate: 20-30 minutes
Question One: Camping Bag Program ⏱️ 20-30 minutes
Scenario: Your school is organizing camping in the forest. You need to pack important items in your backpack (water bottle, flashlight, first aid kit, etc.).
Task: Create an interactive program to manage your camping bag items.
:memo: Step-by-step instructions:
Initialize your bag:
python
bag = [] # Create an empty list
Get the number of items:
python
ask = int(input("How many items you want to put inside your bag? "))
Create the while loop:
- Set condition:
while ask > 0:
- Inside the loop:
- Ask for item name:
item = input("Enter item name: ")
- Add to bag:
bag.append(item)
- Decrease counter:
ask = ask - 1
Display the final bag:
python
print("Items in your bag:", bag)
tip :bulb: Enhancement ideas:
:warning: Important! Before submitting:
- :white_check_mark: Test all your code in the Colab notebook
- :white_check_mark: Verify each output matches the expected results
- :white_check_mark: Save your notebook to your Google Drive
- :white_check_mark: Make sure all exercises are completed
:information_source: :link: Submit your exercise here Your submission helps track your learning progress!
Common issues:
:bulb: Need help? If you're stuck:
- Review the lesson material on loops
- Check your indentation carefully
- Use print statements to debug your code
- Ask your instructor or classmates for guidance