Back

Code Challenge: Playlist Builder

Difficulty  

divider

Objective

Collect song titles until the user is finished, then print the whole playlist back with a track number next to each one.

The Raffle challenge pulled one item out of a list. This one displays all of them — and that turns out to be where the interesting problem is.

Skills to Practice

  • Building a list from user input
  • Looping until a sentinel value is entered
  • Traversing a list
  • Using len() to count

Tasks

  • Create a new Python program named cc-playlist-builder.
  • Keep asking for a song title, adding each one to a list, until the user types done.
  • The word done must not end up on the playlist.
  • Print a header, then every song, each with its track number.
  • Print how many songs are on the playlist.

The Catch

Look carefully at the sample output. The first song is labeled 1, but in the list it lives at index 0. Track numbers and indexes are not the same thing, and it is your job to line them up.

Get this wrong in the obvious way and your playlist starts at track 0 — which no music player on earth does.

Stretch Goals

  • Print the last song on its own, using len() to find it rather than counting by hand.
  • Handle an empty playlist gracefully. If the user types done immediately, say so instead of printing an empty header.

Sample Output

Sample Output
Add a song (or type 'done'): Bohemian Rhapsody [Enter]
Add a song (or type 'done'): Mr. Brightside [Enter]
Add a song (or type 'done'): Take On Me [Enter]
Add a song (or type 'done'): done [Enter]
--- Your Playlist ---
1. Bohemian Rhapsody
2. Mr. Brightside
3. Take On Me
3 songs on this playlist.

Commence Challenge