Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ The First while Loop | Section
Python Loops
セクション 1.  7
single

single

bookThe First while Loop

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

The while loop is used to repeat a block of code as long as a specified condition evaluates to True. The condition is checked at the beginning of each iteration, and the loop stops when the condition becomes False.

while condition:
    # Code to execute while the condition is True

condition: a Boolean expression that evaluates to True or False.

We will print all destinations from the travel_list one by one.

123456789
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Initialize the index i = 0 # Print each destination using a while loop while i < len(travel_list): print(travel_list[i]) i += 1
copy
  • The variable i starts at 0, which represents the first index of the travel_list;
  • The while loop checks if i is less than the length of the list (len(travel_list)). This ensures the loop doesn't exceed the list bounds;
  • The loop prints the destination at the current index travel_list[i];
  • The variable i is incremented by 1 in each iteration using i += 1;
  • When i becomes equal to the length of the list, the condition evaluates to False, and the loop stops.
タスク

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

You are a traveler planning your next adventure. To make things manageable, you decide to visit only half of the countries from a given list.

  • Use a while loop to go through the list, selecting only the first half of the destinations.
  • Use // for integer division to correctly determine the halfway point of the list for the while loop condition.
  • Store your chosen countries in the selected list.

解答

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

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

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

セクション 1.  7
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt