DEV Community

Kumatso1
Kumatso1

Posted on

Sorting and Searching in Python(looking for help)

I wrote this Python code but it is giving me this error shown in the screenshot and I don't know where it is coming from. I am a beginner in Python.

Image description

Program:
"""
This program defines the Album class with the specified attributes and string representation.
Then, it creates two lists albums1 and albums2, populates them with Album objects,
and performs various operations like sorting, swapping, copying, adding elements, and searching.
"""
class Album: # defineing Album class
def init(self, album_name, number_of_songs, album_artist):
self.album_name = album_name
self.number_of_songs = number_of_songs
self.album_artist = album_artist

def __str__(self):  
   return f"({self.album_name}, {self.album_artist}, {self.number_of_songs})"
Enter fullscreen mode Exit fullscreen mode

def main():
albums1 = []
albums1.append(Album("Thriller", 9, "Michael Jackson"))
albums1.append(Album("Back in Black", 10, "AC/DC"))
albums1.append(Album("The Dark Side of the Moon", 10, "Pink Floyd"))
albums1.append(Album("Rumours", 11, "Fleetwood Mac"))
albums1.append(Album("Born to Run", 8, "Bruce Springsteen"))

# Print albums before sorting
print("Albums before sorting:")
for album in albums1:
    print(album)

# Swap elements at positions 1 and 2
albums1[1], albums1[2] = albums1[2], albums1[1]

# Print albums after swapping
print("\nAlbums after swapping positions 1 and 2:")
for album in albums1:
    print(album)

albums2 = []
albums2.append(Album("Purple Rain", 9, "Prince"))        
albums2.append(Album("Like a Prayer", 9, "Madonna"))
albums2.append(Album("Bridge Over Troubled Water", 11, "Simon & Garfunkel"))
albums2.append(Album("Achtung Baby", 11, "U2"))
albums2.append(Album("Nevermind", 14, "Nirvana"))

# Print albums2
print("\nAlbums2:")
for album in albums2:
    print(album)
# Copy all albums from albums1 to albums2
albums2.extend(albums1)

# Add additional albums
albums2.append(Album("Dark Side of the Moon", 9, "Pink Floyd"))
albums2.append(Album("Oops!... I Did It Again", 16, "Britney Spears"))

# Print albums2 after additions
print("\nAlbums2 after additions:")
for album in albums2:
    print(album)

# Sort albums2 alphabetically by album name
albums2.sort(key=lambda album: album.album_name)

# Print albums2 after sorting alphabetically
print("\nAlbums2 after sorting alphabetically:")
for album in albums2:
    print(album)

# Search for Dark Side of the Moon
dark_side_index = albums2.index(Album("Dark Side of the Moon", 9, "Pink Floyd"))
(f"\nIndex of Dark Side of the Moon: {dark_side_index}")
Enter fullscreen mode Exit fullscreen mode

if name == "main":
main()

Top comments (0)