Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using Condition Statements in a while Loop | The while Loop
Python Loops Tutorial
course content

Kursusindhold

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
Using Condition Statements in a while Loop

The if/else structure can be combined with a while loop to add conditional logic for each iteration. This allows you to perform different actions based on specific conditions while the loop executes.

Let's apply this concept to our travel_list. The program will categorize cities as having short or long names depending on their length (less than 8 characters for short, 8 or more for long).

123456789101112
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Initialize index i = 0 # Categorize cities by name length while i < len(travel_list): if len(travel_list[i]) < 8: print(travel_list[i], 'has a short name.') else: print(travel_list[i], 'has a long name.') i += 1
copy
  1. The index i is initialized to 0 to start from the first city;
  2. The while loop runs as long as i is less than the length of the travel_list;
  3. Conditional Logic:
    • if: checks if the length of the current city name is less than 8 characters and prints a message accordingly;
    • else: handles all other cases where the name length is 8 or more characters;
  4. The i variable is incremented at the end of each iteration to move to the next city.
Opgave

Swipe to start coding

You are a developer working on a travel app that displays a list of country names. For design purposes, the app needs to highlight countries with short names. To achieve this, you decide to automate the process.

  • Count the total number of countries in the countries list that have names shorter than 7 characters.
  • Use a while loop to iterate through the country names.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
toggle bottom row

book
Using Condition Statements in a while Loop

The if/else structure can be combined with a while loop to add conditional logic for each iteration. This allows you to perform different actions based on specific conditions while the loop executes.

Let's apply this concept to our travel_list. The program will categorize cities as having short or long names depending on their length (less than 8 characters for short, 8 or more for long).

123456789101112
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Initialize index i = 0 # Categorize cities by name length while i < len(travel_list): if len(travel_list[i]) < 8: print(travel_list[i], 'has a short name.') else: print(travel_list[i], 'has a long name.') i += 1
copy
  1. The index i is initialized to 0 to start from the first city;
  2. The while loop runs as long as i is less than the length of the travel_list;
  3. Conditional Logic:
    • if: checks if the length of the current city name is less than 8 characters and prints a message accordingly;
    • else: handles all other cases where the name length is 8 or more characters;
  4. The i variable is incremented at the end of each iteration to move to the next city.
Opgave

Swipe to start coding

You are a developer working on a travel app that displays a list of country names. For design purposes, the app needs to highlight countries with short names. To achieve this, you decide to automate the process.

  • Count the total number of countries in the countries list that have names shorter than 7 characters.
  • Use a while loop to iterate through the country names.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt