Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- def openFile(filename):
- data = []
- with open(filename,"r") as a:
- data = a.read()
- data = data.split("\n")
- return(data)
- def dictFromFile(filename):
- albums = {}
- data = openFile(filename)
- for i in range(len(data)):
- album,artist,songs,price = data[i].split("|")
- songs = songs.split(",")
- albumClass = albumInfo(album,artist,songs,price)
- albumClass.addDict(albums)
- return(albums)
- class albumInfo():
- def __init__(self,album,artist,songs,price):
- self.album = album
- self.artist = artist
- self.songs = songs
- self.price = price
- def addDict(self,albums):
- albums[self.album] = [self.artist,self.songs,self.price]
- return(albums)
- def searchDict(dictionary,find):
- for key,value in dictionary.items():
- if find in dictionary[key][1]:
- print("Found {} in Album: {}".format(find,key))
- return
- print("Did not find your song D: ")
- def getTotal(albums):
- total = 0
- for i in albums:
- total = total + float(albums[i][2])
- return(total)
- def run():
- start_time = time.time()
- albums = dictFromFile("info.txt")
- keepGoing = True
- while keepGoing:
- ask = "S"
- if ask in ["A","S","T","Q"]:
- if ask == "T":
- print("The total value of your libray is: $"+str(getTotal(albums)))
- elif ask == "S":
- find = "Moonlight"
- searchDict(albums,find)
- print("My program took", time.time() - start_time, "to run")
- elif ask == "A":
- text = input("Please add your album in this format: AlbumName|ArtistName|Song1,Song2,Song3|Cost")
- album,artist,songs,price = text.split("|")
- songs = songs.split(",")
- albumClass = albumInfo(album,artist,songs,price)
- albumClass.addDict(albums)
- else:
- keepGoing = False
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement