Below is a simple Python code snippet that reads song durations and names from input, creates a Song object for each entry, and adds it to the myplaylist list.
The input is assumed to be in the format of alternating lines of duration and name:
class Song:
def __init__(self, duration, name):
self.duration = duration
self.name = name
# Function to read song durations and names from input
def read_songs():
myplaylist = []
while True:
duration_input = input("Enter song duration (or -1 to stop): ")
duration = int(duration_input)
if duration == -1:
break
name = input("Enter song name: ")
currsong = Song(duration, name)
myplaylist.append(currsong)
return myplaylist
# Example usage
myplaylist = read_songs()
# Print the playlist
print("My Playlist:")
for song in myplaylist:
print(f"{song.name}: {song.duration} seconds")