Decisions and loops
Project: seven-day study plan
Combine a for loop, conditions, and a running total in a weekly study plan.
Lesson 17 of 63
Reading: about 2 min
Lesson + practice: about 17 min
Difficulty: Beginner
On this page4 sections
Create weekly_plan.py. Plan 20 minutes on weekdays and 40 minutes on weekend days. Use day numbers 1 through 7, with 6 and 7 representing the weekend.
Build the plan
total_minutes = 0
for day in range(1, 8):
if day >= 6:
minutes = 40
else:
minutes = 20
total_minutes += minutes
print(f"Day {day}: {minutes} min")
print(f"Weekly total: {total_minutes} min")Day 1: 20 min Day 2: 20 min Day 3: 20 min Day 4: 20 min Day 5: 20 min Day 6: 40 min Day 7: 40 min Weekly total: 180 min
Why this works
The loop visits seven day numbers. The condition selects 40 minutes for days 6 and 7, and 20 minutes for days 1 through 5. The total is updated once per day, so five weekday additions and two weekend additions produce 5 × 20 + 2 × 40 = 180 minutes.
Check and extend
Run the program and confirm the weekly total is 180 minutes. Change weekend minutes to 30 and predict the new total before rerunning. It should become 160 minutes.
Why is total_minutes assigned before the loop?
Reveal answer
The running total must start at zero and retain its value across all seven iterations.
Try another case
If weekdays become 25 minutes and weekends stay 40, what is the total?
Check your answer
Reveal answer
Five weekdays contribute 125 minutes and two weekend days contribute 80, for 205 minutes.
Your progress
Reveal the check answer or reach the end to complete this lesson.