Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import time
  2.  
  3.  
  4. def openFile(filename):
  5. data = []
  6. with open(filename,"r") as a:
  7. data = a.read()
  8. data = data.split("\n")
  9. return(data)
  10.  
  11. def dictFromFile(filename):
  12. albums = {}
  13. data = openFile(filename)
  14. for i in range(len(data)):
  15. album,artist,songs,price = data[i].split("|")
  16. songs = songs.split(",")
  17. albumClass = albumInfo(album,artist,songs,price)
  18. albumClass.addDict(albums)
  19. return(albums)
  20.  
  21. class albumInfo():
  22. def __init__(self,album,artist,songs,price):
  23. self.album = album
  24. self.artist = artist
  25.  
  26. self.songs = songs
  27. self.price = price
  28.  
  29. def addDict(self,albums):
  30. albums[self.album] = [self.artist,self.songs,self.price]
  31. return(albums)
  32.  
  33. def searchDict(dictionary,find):
  34. for key,value in dictionary.items():
  35. if find in dictionary[key][1]:
  36. print("Found {} in Album: {}".format(find,key))
  37. return
  38. print("Did not find your song D: ")
  39.  
  40. def getTotal(albums):
  41. total = 0
  42. for i in albums:
  43. total = total + float(albums[i][2])
  44. return(total)
  45.  
  46. def run():
  47. start_time = time.time()
  48. albums = dictFromFile("info.txt")
  49. keepGoing = True
  50. while keepGoing:
  51. ask = "S"
  52. if ask in ["A","S","T","Q"]:
  53. if ask == "T":
  54. print("The total value of your libray is: $"+str(getTotal(albums)))
  55. elif ask == "S":
  56. find = "Moonlight"
  57. searchDict(albums,find)
  58. print("My program took", time.time() - start_time, "to run")
  59. elif ask == "A":
  60. text = input("Please add your album in this format: AlbumName|ArtistName|Song1,Song2,Song3|Cost")
  61. album,artist,songs,price = text.split("|")
  62. songs = songs.split(",")
  63. albumClass = albumInfo(album,artist,songs,price)
  64. albumClass.addDict(albums)
  65. else:
  66. keepGoing = False
  67.  
  68.  
  69. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement