Course Content
Python Loops Tutorial
Python Loops Tutorial
Nested while Loop
In real life, you may need to organize or analyze data with multiple levels, such as tracking expenses for different trips. A nested while loop allows you to process these multi-dimensional scenarios efficiently when the number of iterations isn't predetermined.
Example: Organizing Travel Expenses
Imagine you have multiple trips, and each trip has a list of expenses (flights, hotels, food, etc.). Using a nested while
loop, you can calculate the total cost for each trip.
# List of trips with their respective expenses travel_costs = [ [500, 200, 100, 150], # Trip 1: Flights, Hotels, Food, Activities [600, 250, 120, 200], # Trip 2: Flights, Hotels, Food, Activities [550, 180, 130, 170] # Trip 3: Flights, Hotels, Food, Activities ] # Initialize the outer loop to iterate over trips i = 0 while i < len(travel_costs): total_cost = 0 # Reset the total cost for the current trip j = 0 # Inner loop to iterate over expenses in each trip while j < len(travel_costs[i]): total_cost += travel_costs[i][j] # Add the expense to the total cost j += 1 # Print the total cost for the current trip print(f"Total cost for Trip {i + 1}: ${total_cost}") i += 1 # Move to the next trip
Explanation
- Outer Loop (
while i < len(travel_costs)
): iterates through the list of trips, where each row represents the expenses for a single trip; - Inner Loop (
while j < len(travel_costs[i])
): iterates through the expenses for the current trip, summing them up in thetotal_cost
variable; - Print Results: after summing up expenses for a trip, the program prints the total cost for that trip;
- Move to the Next Trip: increment
i
to analyze the next trip until all trips are processed; - Final Output: after the loop completes, print the trip number with the highest total cost and its value.
Note
Nested loops are commonly used to iterate through n-dimensional matrices, where each level of nesting represents a dimension. In our tasks, we are working with a 2-dimensional matrix, such as a list of trips and their expenses. Nested loops are also helpful for dynamic looping scenarios, like input validation or navigating multi-level menus, where the number of iterations isn’t fixed and depends on changing conditions or user input.
Swipe to show code editor
Increment every element in the matrix by 1
.
Write a program using nested while
loops to:
- Calculate the total cost for each trip.
- Find and print the trip number with the highest total cost.
Expected Output:
Thanks for your feedback!
Nested while Loop
In real life, you may need to organize or analyze data with multiple levels, such as tracking expenses for different trips. A nested while loop allows you to process these multi-dimensional scenarios efficiently when the number of iterations isn't predetermined.
Example: Organizing Travel Expenses
Imagine you have multiple trips, and each trip has a list of expenses (flights, hotels, food, etc.). Using a nested while
loop, you can calculate the total cost for each trip.
# List of trips with their respective expenses travel_costs = [ [500, 200, 100, 150], # Trip 1: Flights, Hotels, Food, Activities [600, 250, 120, 200], # Trip 2: Flights, Hotels, Food, Activities [550, 180, 130, 170] # Trip 3: Flights, Hotels, Food, Activities ] # Initialize the outer loop to iterate over trips i = 0 while i < len(travel_costs): total_cost = 0 # Reset the total cost for the current trip j = 0 # Inner loop to iterate over expenses in each trip while j < len(travel_costs[i]): total_cost += travel_costs[i][j] # Add the expense to the total cost j += 1 # Print the total cost for the current trip print(f"Total cost for Trip {i + 1}: ${total_cost}") i += 1 # Move to the next trip
Explanation
- Outer Loop (
while i < len(travel_costs)
): iterates through the list of trips, where each row represents the expenses for a single trip; - Inner Loop (
while j < len(travel_costs[i])
): iterates through the expenses for the current trip, summing them up in thetotal_cost
variable; - Print Results: after summing up expenses for a trip, the program prints the total cost for that trip;
- Move to the Next Trip: increment
i
to analyze the next trip until all trips are processed; - Final Output: after the loop completes, print the trip number with the highest total cost and its value.
Note
Nested loops are commonly used to iterate through n-dimensional matrices, where each level of nesting represents a dimension. In our tasks, we are working with a 2-dimensional matrix, such as a list of trips and their expenses. Nested loops are also helpful for dynamic looping scenarios, like input validation or navigating multi-level menus, where the number of iterations isn’t fixed and depends on changing conditions or user input.
Swipe to show code editor
Increment every element in the matrix by 1
.
Write a program using nested while
loops to:
- Calculate the total cost for each trip.
- Find and print the trip number with the highest total cost.
Expected Output:
Thanks for your feedback!