Nested 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)
- 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.
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.
- You are given a list
travel_costs
, where each sublist represents a single tripβs expenses. - For each trip, identify the highest expense.
- Store the highest expenses in the
max_costs
list.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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?
Awesome!
Completion rate improved to 5
Nested 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)
- 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.
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.
- You are given a list
travel_costs
, where each sublist represents a single tripβs expenses. - For each trip, identify the highest expense.
- Store the highest expenses in the
max_costs
list.
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 5single