Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer break/continue in a Nested Loop | Nested Loops
Python Loops Tutorial
course content

Cursusinhoud

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
break/continue in a Nested Loop

Let's apply the concepts of break and continue to analyze travel costs practically. We'll combine a while loop and a for loop to process expenses across multiple trips.

Imagine you have multiple trips, and each trip has a list of expenses. If any expense exceeds a specific budget threshold, we will stop processing that trip immediately.

12345678910111213141516171819202122232425
# List of trips with their respective expenses travel_costs = [ [100, 150, 300, 50], # Trip 1 [200, 500, 100, 80], # Trip 2 [120, 180, 400, 150] # Trip 3 ] # Budget threshold budget = 200 # Outer while loop to iterate through trips i = 0 while i < len(travel_costs): print(f"Processing expenses for Trip {i + 1}:") # Inner for loop to iterate through expenses for cost in travel_costs[i]: # If expense exceeds the budget if cost > budget: print('Expense', cost, 'exceeds the budget. Stopping this trip.') break print('Expense:', cost) i += 1 # Move to the next trip print('') # Add a new line for readability
copy
  • Outer loop: iterates through the list of trips using the index i;
  • Inner loop: processes each expense in the current trip;
  • break in the inner loop: if an expense exceeds the budget, the break statement stops processing expenses for the current trip.
Taak

Swipe to start coding

You are analyzing travel expenses from multiple trips, where each trip contains a list of expenses for transportation, accommodation, food, and activities. Your goal is to identify the first significant expense in each trip while applying specific filtering rules.

  • Iterate through each trip's expenses one by one.
  • Skip expenses below $100, as they are not considered significant.
  • Stop at the first expense greater than $200 using break.
  • Store this first significant expense from each trip in the significant_expenses list.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 4
toggle bottom row

book
break/continue in a Nested Loop

Let's apply the concepts of break and continue to analyze travel costs practically. We'll combine a while loop and a for loop to process expenses across multiple trips.

Imagine you have multiple trips, and each trip has a list of expenses. If any expense exceeds a specific budget threshold, we will stop processing that trip immediately.

12345678910111213141516171819202122232425
# List of trips with their respective expenses travel_costs = [ [100, 150, 300, 50], # Trip 1 [200, 500, 100, 80], # Trip 2 [120, 180, 400, 150] # Trip 3 ] # Budget threshold budget = 200 # Outer while loop to iterate through trips i = 0 while i < len(travel_costs): print(f"Processing expenses for Trip {i + 1}:") # Inner for loop to iterate through expenses for cost in travel_costs[i]: # If expense exceeds the budget if cost > budget: print('Expense', cost, 'exceeds the budget. Stopping this trip.') break print('Expense:', cost) i += 1 # Move to the next trip print('') # Add a new line for readability
copy
  • Outer loop: iterates through the list of trips using the index i;
  • Inner loop: processes each expense in the current trip;
  • break in the inner loop: if an expense exceeds the budget, the break statement stops processing expenses for the current trip.
Taak

Swipe to start coding

You are analyzing travel expenses from multiple trips, where each trip contains a list of expenses for transportation, accommodation, food, and activities. Your goal is to identify the first significant expense in each trip while applying specific filtering rules.

  • Iterate through each trip's expenses one by one.
  • Skip expenses below $100, as they are not considered significant.
  • Stop at the first expense greater than $200 using break.
  • Store this first significant expense from each trip in the significant_expenses list.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 4
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt