Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Nested while Loop | Nested Loops
Python Loops Tutorial

bookNested while Loop

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.

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.

12345678910111213141516171819202122232425262728293031323334
# 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 ] # Variables to track the maximum cost max_cost = 0 max_trip = 0 # Outer loop to iterate over trips i = 0 while i < len(travel_costs): total_cost = 0 j = 0 # Inner loop to iterate over expenses in each trip while j < len(travel_costs[i]): total_cost += travel_costs[i][j] j += 1 # Print the total cost for the current trip print('Total cost for Trip', i + 1, ':', total_cost) # Check if this trip is the new maximum if total_cost > max_cost: max_cost = total_cost max_trip = i + 1 i += 1 # Final output: print the trip with the highest total cost print("Trip", max_trip, "has the highest total cost of", max_cost)
copy
  • 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 the total_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.
Task

Swipe to start coding

You are analyzing a dataset of travel costs, where each sublist represents the expenses for a single trip. Each trip includes various costs such as flights, hotels, food, and activities. Since expenses vary, you need to identify the highest expense for each trip to track major costs.

  1. You are given a list travel_costs, where each sublist represents a single trip’s expenses.
  2. For each trip, identify the highest expense.
  3. Store the highest expenses in the max_costs list.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how the nested while loops work in this example?

What would change if the number of expenses per trip was different for each trip?

Can you show how to modify this code to also calculate the average cost per trip?

close

Awesome!

Completion rate improved to 5

bookNested while Loop

Swipe to show menu

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.

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.

12345678910111213141516171819202122232425262728293031323334
# 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 ] # Variables to track the maximum cost max_cost = 0 max_trip = 0 # Outer loop to iterate over trips i = 0 while i < len(travel_costs): total_cost = 0 j = 0 # Inner loop to iterate over expenses in each trip while j < len(travel_costs[i]): total_cost += travel_costs[i][j] j += 1 # Print the total cost for the current trip print('Total cost for Trip', i + 1, ':', total_cost) # Check if this trip is the new maximum if total_cost > max_cost: max_cost = total_cost max_trip = i + 1 i += 1 # Final output: print the trip with the highest total cost print("Trip", max_trip, "has the highest total cost of", max_cost)
copy
  • 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 the total_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.
Task

Swipe to start coding

You are analyzing a dataset of travel costs, where each sublist represents the expenses for a single trip. Each trip includes various costs such as flights, hotels, food, and activities. Since expenses vary, you need to identify the highest expense for each trip to track major costs.

  1. You are given a list travel_costs, where each sublist represents a single trip’s expenses.
  2. For each trip, identify the highest expense.
  3. Store the highest expenses in the max_costs list.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

close

Awesome!

Completion rate improved to 5
SectionΒ 3. ChapterΒ 2
single

single

some-alt