セクション 1. 章 16
single
ネストされたループ内のbreak/continue
メニューを表示するにはスワイプしてください
break と continue の概念を旅行費用の実践的な分析に適用する。while ループと for ループを組み合わせて、複数の旅行にわたる費用を処理する。
複数の旅行があり、各旅行には費用のリストがある。任意の費用が特定の予算を超えた場合、その旅行の処理が直ちに停止される。
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
- 外側ループ: インデックス
iを使用して旅行リストを反復処理する; - 内側ループ: 現在の旅行の各費用を処理する;
- 内側ループでの
break: 費用がbudgetを超えた場合、break文によって当該旅行の費用処理が停止される。
タスク
スワイプしてコーディングを開始
複数の旅行からの旅行費用を分析しています。各旅行には、交通費、宿泊費、食費、アクティビティ費用のリストが含まれています。目的は、特定のフィルタリングルールを適用しながら、各旅行で最初の重要な支出を特定することです。
- 各旅行の費用を順番に反復処理します。
- 100ドル未満の費用はスキップします。これらは重要な支出とは見なされません。
- 200ドルを超える最初の費用で
breakを使用して処理を停止します。 - 各旅行からこの最初の重要な支出を
significant_expensesリストに保存します。
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 16
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください