Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ ネストされたループ内のbreak/continue | セクション
Pythonのループ
セクション 1.  16
single

single

bookネストされたループ内のbreak/continue

メニューを表示するにはスワイプしてください

breakcontinue の概念を旅行費用の実践的な分析に適用する。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
copy
  • 外側ループ: インデックス i を使用して旅行リストを反復処理する;
  • 内側ループ: 現在の旅行の各費用を処理する;
  • 内側ループでの break: 費用が budget を超えた場合、break 文によって当該旅行の費用処理が停止される。
タスク

スワイプしてコーディングを開始

複数の旅行からの旅行費用を分析しています。各旅行には、交通費、宿泊費、食費、アクティビティ費用のリストが含まれています。目的は、特定のフィルタリングルールを適用しながら、各旅行で最初の重要な支出を特定することです。

  • 各旅行の費用を順番に反復処理します。
  • 100ドル未満の費用はスキップします。これらは重要な支出とは見なされません。
  • 200ドルを超える最初の費用でbreakを使用して処理を停止します。
  • 各旅行からこの最初の重要な支出を significant_expenses リストに保存します。

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  16
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt