1

So I am given a CSV file which catalogs some 1000 songs, the data given is the Artist, Album, Song_Title, and Duration of the songs in seconds.

I import the CSV file by doing the following:

column_names = ['artist', 'album', 'song_title', 'duration']

track_list = pd.read_csv('/Users/Basel/Desktop/Class Files/JukeBoxHero/music-collection.csv', names=column_names,
                         header=None)

for reference, this is an example of a line from the CSV file:

Aerosmith,A Little South Of Sanity Disc 1,Falling In Love (Is Hard On The Knees),209

The function I am attempting to use to allow the user to enter a search term and get a list of all the songs which have the search term in their names goes as follows:

def findSongs(songList):
    searchTerm=input("searching for?")
    for songs in songList:
        if searchTerm in songs:
            print("-----------------------")
            print("Artist: " + currentSong['artist'])
            print("Album: " + currentSong['album'])
            print("Title: " + currentSong['song_title])
            print("Duration: " + currentSong['duration'] + "seconds")

However, when I try to call the function with print(findSongs(track_list)) the terminal always seems to output None. My guess is that there is something wrong in the way I am structuring my for-loop but I have spent a lot of time trying out different variables and trying to use integer indexes all to no avail.

1 Answer 1

1

Try this:

def findSongs(songList):
    searchTerm=input("searching for?")
    songList = songList[songList.song_title.str.contains(searchTerm)].to_numpy()
    for songs in songList:
            print("-----------------------")
            print("Artist: " + songs[0])
            print("Album: " + songs[1])
            print("Title: " + songs[2])
            print("Duration: " + str(songs[3]))

findSongs(track_list)

output:

searching for?Falling
-----------------------
Artist: Aerosmith
Album: A Little South Of Sanity Disc 1
Title: Falling In Love (Is Hard On The Knees)
Duration: 209

updated version:

def findSongs(songList):
  while True:
    searchTerm=input("searching for ( exit to exit )?")
    if searchTerm == 'exit':
       break
    songList = songList[songList.song_title.str.contains(searchTerm)].to_numpy()
    if len(songList) == 0:
       print("No Song found ")
       continue
    for songs in songList:
            print("-----------------------")
            print("Artist: " + songs[0])
            print("Album: " + songs[1])
            print("Title: " + songs[2])
            print("Duration: " + str(songs[3]))
Sign up to request clarification or add additional context in comments.

3 Comments

This solves my problem but is there a way to put this in an if-else context, however? The end-intent of having the if was to have an else prompting the user that no songs were found if there are no matching search terms.
@Banani720 i updated the answer with one that continues asking until exit is input and will let users know that no answer was found. I hope that helps, thx!
Yes and the answer makes a lot of sense conceptually as well appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.