Categories
Mastering Development

Python function and if…else

The goal is to create a function to receive any word as input, search songs only, and return the number of top songs that contain that word. The function should be able to receive and word/phrase as an input string and return a message (i included one in my code); if the word/phrase is not found, it must return a message saying no songs were found with "word/phrase;" has to be able to deal with upper/lower case letters. At the end, I have to test the function with the word "like," which should result in 100 songs.

Data set: https://raw.githubusercontent.com/walkerkq/musiclyrics/master/billboard_lyrics_1964-2015.csv

My code is:

def no_top_songs(word):
    for song in lyrics['Song']:
        count=0
        if word in lyrics:
            count=count+1
        if count!=0:
            print(f'{str(count)} songs were found to contain {word} in this dataset')
        else:
            print(f'No songs were found to contain the words:{word}')
  
     print(no_top_songs('like'))

Output is hundreds of rows of "No songs were found to contain the words:like" and then one row of "None"

Please help. I am not sure what I am doing wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *