-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_manager.py
51 lines (43 loc) · 1.66 KB
/
music_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import csv
import random
class MusicManager:
def __init__(self, filename):
self.songs = []
self.load_songs(filename)
def add_song(self, title, artist, year):
with open('songs.csv', 'a', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([f"'{title}'", f"'{artist}'", year])
self.songs.append((title, artist, year))
print("Song added successfully!")
def search_by_title(self, title):
found = False
for song in self.songs:
if song[0].lower() == title.lower():
print(f"{song[0]} - {song[1]} ({song[2]})")
found = True
if not found:
print("No songs found with this title.")
def search_by_year(self, year):
found = False
for song in self.songs:
if song[2] == year:
print(f"{song[0]} - {song[1]} ({song[2]})")
found = True
if not found:
print("No songs found for this year.")
def random_playlist(self):
if len(self.songs) == 0:
print("There are no songs registered.")
return
playlist = random.sample(self.songs, len(self.songs))
print("Random playlist:")
for i, song in enumerate(playlist):
print(f"{i+1}. {song[0]} - {song[1]} ({song[2]})")
def load_songs(self, filename):
with open(filename, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for row in reader:
title, artist, year = row
self.songs.append((title, artist, int(year)))