Song list

Assignment

Design Information

You are required to implement and use at least the following ten functions for Project 1, in addition to main(). You may implement more functions if you think them necessary, but the ten below must be implemented and used. The information for each function is below.

Printing Functions
As the name suggests, these functions print information to the user. None of them return values – they only print information, which does NOT count as output. Some of them take in parameters, while others do not.

  • defdisplayMainMenu()

o Prints out the main menu of the program
What would you like to do next? 0) Perform a search
1) Create a playlist

o Takes in nothing (the menu is always the same) o Returns nothing, since it is a print function

  • defdisplayOptions()

o Prints out a list of the six different attributes shown for each song 0 – Year

1 – Artist              2 – Title              3 – Genre              4 – Duration              5 – Tempo

o Takes in nothing (the menu is always the same) o Returns nothing, since it is a print function

  • defdisplayPLOptions()

o Prints out the three different options for creating a playlist

1 – Year

2 – Artist

3 – Genre

o Takes in nothing (the menu is always the same) o Returns nothing, since it is a print function

  • defprintSongs(songs)
    o Prints the details of every song in the given 2D list

Year – Genre – Artist – Song Title

o Takes in a 2D list of songs
§Each song is a separate interior list (i.e., a row)

o Returns nothing, since it is a print function

Helper Functions

As the name suggests, these functions “help” other functions, by performing small tasks that will be needed by many pieces of the program. They are often called from functions other than main().

  • defgetValidInput(prompt, minimum, maximum)
    o Gets a valid integer from the user that falls within a certain range (between minimum and maximum, inclusive), using the provided

string of prompt when asking the user for input. o Takes in a prompt and two integers

  • The prompt is used by the function when asking for input
  • The two integers are the min and max values (inclusive) o Returns an integer
  • defcolToString(column)
    o Converts a number to the corresponding column heading

0 becomes Year
1
becomes Artist 2 becomes Title
3
becomes Genre
4
becomes Duration 5 becomes Tempo

o Takes in an integer, the index of a column
o Returns a string containing the column heading

  • defsongToString(song)
    o Converts a song’s information into a string of its details

Year – Genre – Artist – Song Title

o Takes in a 1D list containing the details of a single song o Return value is the final string with the details

  • For example:
    “1994 – rock – Tom Petty – A Higher Place”

General Functions

These are the “heavy lifters” of the program, and do the majority of the work, and are almost always called from main(). They often call other functions, often more than one.

  • def search(songs, col, value)

Create a 2D list of all the songs that match the value being searched for in the selected column
Takes in the list of songs, a column, and a value to search for

  • § col is an integer between 0 and 5 (representing year, artist, title, genre, duration, or tempo, respectively)
  • § value is the value being searched for
    Returns a 2D list of all the songs that match the value in the selected column
  • § If the column selected is tempo or duration, search() should return all songs where that column is greater than or equal to the value being searched for
  • § For all other columns, the match must be exact (including case sensitivity)
  • defcreatePlaylist(songs, choice, length)
    o Creates a 2D list of all songs within the type selected by the user;

limits the list to a length of length
o Takes in the list of songs, a choice, and a length for the playlist

  • § choice is an integer 1 and 3 (representing the playlist options of year, artist, or genre, respectively)
  • § length is a positive integer between 0 and 10
  • The maximum length of a playlist is 10 songs.
    o Within the function, the user should be prompted to enter either the year, artist, or genre a playlist should be comprised of
    o Returns a 2D list of the first length songs that match the value for the selected choice
    §For example, if the user selected year and 3 songs, then chose 1999 as the year, the first three songs from 1999 should be returned.
  • def make2DList(filename) o Takes in the filename as a string

o Reads in the contents of the song library file
o Stores the results in a 2D list called resultList

o You can find it in Dr. Gibson’s public directory
o Make sure to include the provided function header.

  • You can delete the table showing the data organization (after the “Output” line) if you like.

o Do not change the code – it works perfectly as is!

Solution

rashed.py

 # main menu options

USER_QUIT    = -1

MENU_SEARCH  = 0

MENU_CREATE  = 1

MAX_SONG_NUM = 10  # maximum number of songs in a playlist

# these constants are used to give names to the columns of the 2D list

YEAR     = 0

ARTIST   = 1

TITLE    = 2

GENRE    = 3

DURATION = 4

TEMPO    = 5

DETAIL_MIN = YEAR   # minimum value; used for getValidInput()

DETAIL_MAX = TEMPO  # maximum value

importcsv

def make2DList(filename):

fileObj = open(filename)

lines = fileObj.readlines()

fileObj.close()

resultList = []

index = 0

while index <len(lines):

line = lines[index].strip().split(“,”)

line[YEAR]     = int(line[YEAR])

line[DURATION] = float(line[DURATION])

line[TEMPO]    = float(line[TEMPO])

resultList.append(line)

index += 1

returnresultList

defdisplayMainMenu():

print (“What would you like to do next?\n0) Perform a search\n1) Create a playlist\n”)

defdisplayOptions():

print (“0 – Year\n1 – Artist\n2 – Title\n3 – Genre\n4 – Duration\n5 – Tempo\n”)

defdisplayPLOptions():

print (‘What playlist type do you want to create?’)

print (“(1) Year Playlist\n2) Artist Playlist\n3) Genre Playlist”)

defcolToString(column):

if column == 0:

return ‘year’

if column == 1:

return ‘artist’

if column == 2:

return ‘title’

if column == 3:

return ‘genre’

if column == 4:

return ‘duration’

if column == 5:

return ‘tempo’

# In[4]:

defgetValidInput(prompt, minimum, maximum):

inpt = int(input(prompt))

while ((inpt< minimum) or (inpt> maximum)):

print (‘You entered invalid input’)

inpt = input(prompt)

returninpt

# In[5]:

defsongToString(song):

print (song[0] + ” – ” + song[3] +  ” – ” + song[1] +  ” – ” + song[2])

# In[6]:

defprintSongs(songs):

for song in songs:

print (str(song[0]) + ” – ” + song[3] +  ” – ” + song[1] +  ” – ” + song[2])

# In[7]:

def search(resultList, col, value):

new_list = []

i = 0

if (col== 4 or col == 5):

while i<len(resultList):

if (resultList[i][col]>=int(value)):

new_list.append(resultList[i])

i= i+1

returnnew_list

elif (col ==0):

while i<len(resultList):

if ((resultList[i][col])== eval(value)):

new_list.append(resultList[i])

i = i + 1

returnnew_list

else:

while i<len(resultList):

if (resultList[i][col] == value):

new_list.append(resultList[i])

i = i+1

returnnew_list

# In[8]:

defcreatePlaylist(songs, choice, length):

if choice == 1:

col = 0

if choice == 2:

col = 1

if choice == 3:

col = 3

value_input = input(‘Enter a %s to make a playlist of:’ %(colToString(col)))

if (is_in_data(songs, col, value_input)==0):

print (‘Sorry, the %s %s doesnt exist in your library.\nUnable to create a playlsit based on that criteria.’ %(colToString(col),value_input))

return

print (‘Created this playlist:’)

print(printSongs( search(songs, col, value_input)[:length]))

# In[9]:

defis_in_data(resultList, col, value):

i = 0

while i<len(resultList):

if (resultList[i][col]== value or str(resultList[i][col])== value):

return 1

i = i + 1

return 0

# In[14]:

def main():

print (‘This is the Music Organizer 3000!’)

filename = input(‘Enter the filename of your song library:’)

resultList = make2DList(filename)

displayMainMenu()

inpt = int(input(“Enter a menu choice (0 – 1) or ‘-1’ to exit:”))

while (inpt!= -1):

while (inpt not in [0, 1]):

print (‘You entered invalid input.’)

inpt = int(input(“Enter a menu choice (0 – 1) or ‘-1’ to exit:”))

ifinpt == -1:

print (‘Thanks for using the Music Organizer 3000, come again!’)

ifinpt == 0:

displayOptions()

inpt = int(input(‘Enter a column choice (0 – 5):’))

while (inpt not in [0,1,2,3,4,5]):

print (‘You entered invalid input.’)

inpt = int(input(‘Enter a column choice (0 – 5):’))

value_input = input(‘Enter the value you want to search for %s:’ %(colToString(inpt)))

print (‘Found the following:’)

result =  (search(resultList, inpt, value_input ))

printSongs(result)

elifinpt == 1:

displayPLOptions()

type_of_playlist = int(input(‘Choose a playlist to make: ‘))

prompt =’Enter the length of your playlist (0-10):’

length = getValidInput(prompt, 0, 10)

createPlaylist(resultList, type_of_playlist, length)

print (‘\n’)

displayMainMenu()

inpt = int(input(“Enter a menu choice (0 – 1) or ‘-1’ to exit:”))

print (‘Thanks for using the Music Organizer 3000, come again!’)

main()

# In[ ]: