Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære While Loops | Section 2
Python - Music

Sveip for å vise menyen

book
While Loops

Today, we'll explore the while loop in Python, a powerful tool that allows you to repeat actions until a certain condition is met. This concept is essential in data analytics, where you often need to process data iteratively.

In Python, a while loop continues to execute a block of code as long as a specified condition is true. Think of it like a musician practicing a section of a song repeatedly until they master it. The loop will keep running until the condition becomes false.

Basic Structure:

python

Example:

Imagine you're tracking the practice time of a musician. You want to keep practicing until a certain number of hours is reached:

12345678
practice_hours = 0 goal_hours = 5 while practice_hours < goal_hours: print("Keep practicing!") practice_hours += 1 print("Practice goal reached!")
copy

In this example, the loop will print "Keep practicing!" and increment practice_hours by 1 until it reaches goal_hours. Once the condition practice_hours < goal_hours is no longer true, the loop stops, and "Practice goal reached!" is printed.

Let's say you have a list of song durations and you want to calculate the total duration until a certain limit is reached:

12345678910
song_durations = [3, 4, 5, 2, 6] # in minutes total_duration = 0 limit = 10 index = 0 while index < len(song_durations) and total_duration + song_durations[index] <= limit: total_duration += song_durations[index] index += 1 print("Total Duration:", total_duration, "minutes")
copy

In this example, the loop iterates through the song_durations list, adding each duration to total_duration until the limit is reached.

Oppgave

Swipe to start coding

Complete the count_popular_tracks function that counts the number of tracks with a popularity score greater than a specified threshold. This function will utilize a while loop to iterate over the tracks dataset.

Inputs:

  • tracks: A list of dictionaries, where each dictionary represents a track and contains a key 'track_popularity' with an integer value.
  • threshold: An integer representing the popularity score threshold.

Steps:

  • Iterate Over Tracks:

    • Use a while loop to iterate over the tracks list until the end of the list is reached.
    • Inside the loop, check if the popularity score of the current track (accessed via tracks[index]['track_popularity']) is greater than the threshold.
  • Update Count: If the condition is met, increment the count by 1.

  • Increment Index: Increment the index by 1 to move to the next track.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
py

solution.py

py

main.py

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

book
While Loops

Today, we'll explore the while loop in Python, a powerful tool that allows you to repeat actions until a certain condition is met. This concept is essential in data analytics, where you often need to process data iteratively.

In Python, a while loop continues to execute a block of code as long as a specified condition is true. Think of it like a musician practicing a section of a song repeatedly until they master it. The loop will keep running until the condition becomes false.

Basic Structure:

python

Example:

Imagine you're tracking the practice time of a musician. You want to keep practicing until a certain number of hours is reached:

12345678
practice_hours = 0 goal_hours = 5 while practice_hours < goal_hours: print("Keep practicing!") practice_hours += 1 print("Practice goal reached!")
copy

In this example, the loop will print "Keep practicing!" and increment practice_hours by 1 until it reaches goal_hours. Once the condition practice_hours < goal_hours is no longer true, the loop stops, and "Practice goal reached!" is printed.

Let's say you have a list of song durations and you want to calculate the total duration until a certain limit is reached:

12345678910
song_durations = [3, 4, 5, 2, 6] # in minutes total_duration = 0 limit = 10 index = 0 while index < len(song_durations) and total_duration + song_durations[index] <= limit: total_duration += song_durations[index] index += 1 print("Total Duration:", total_duration, "minutes")
copy

In this example, the loop iterates through the song_durations list, adding each duration to total_duration until the limit is reached.

Oppgave

Swipe to start coding

Complete the count_popular_tracks function that counts the number of tracks with a popularity score greater than a specified threshold. This function will utilize a while loop to iterate over the tracks dataset.

Inputs:

  • tracks: A list of dictionaries, where each dictionary represents a track and contains a key 'track_popularity' with an integer value.
  • threshold: An integer representing the popularity score threshold.

Steps:

  • Iterate Over Tracks:

    • Use a while loop to iterate over the tracks list until the end of the list is reached.
    • Inside the loop, check if the popularity score of the current track (accessed via tracks[index]['track_popularity']) is greater than the threshold.
  • Update Count: If the condition is met, increment the count by 1.

  • Increment Index: Increment the index by 1 to move to the next track.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt