Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele While Loop | Section 2
Python - Sport

Pyyhkäise näyttääksesi valikon

book
While Loop

The while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute as long as the condition remains true, making it ideal for scenarios where the number of iterations is not predetermined.

Imagine you're analyzing a series of basketball games and want to find the first game where a team scores more than 100 points. You can use a while loop to iterate through the list of games until you find the desired result.

12345678910
games_scores = [95, 102, 88, 110, 99] index = 0 while index < len(games_scores) and games_scores[index] <= 100: index += 1 if index < len(games_scores): print(f"First game with more than 100 points: Game {index + 1} with {games_scores[index]} points") else: print("No game with more than 100 points found")
copy

In this example, the while loop iterates through the list of scores, checking each score until it finds one greater than 100. The loop stops as soon as the condition is no longer met.

Tehtävä

Swipe to start coding

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

book
While Loop

The while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute as long as the condition remains true, making it ideal for scenarios where the number of iterations is not predetermined.

Imagine you're analyzing a series of basketball games and want to find the first game where a team scores more than 100 points. You can use a while loop to iterate through the list of games until you find the desired result.

12345678910
games_scores = [95, 102, 88, 110, 99] index = 0 while index < len(games_scores) and games_scores[index] <= 100: index += 1 if index < len(games_scores): print(f"First game with more than 100 points: Game {index + 1} with {games_scores[index]} points") else: print("No game with more than 100 points found")
copy

In this example, the while loop iterates through the list of scores, checking each score until it finds one greater than 100. The loop stops as soon as the condition is no longer met.

Tehtävä

Swipe to start coding

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt