2021年3月2日星期二

How to add new variables into objects to use in classes

I have the code:

import timedelta  class Tune:        def __init__(self, title, artist, duration):          self.title = title          self.artist = artist          self.duration = duration    class Play:        def __init__(self):          self.tracks = []        def add(self, song):          self.tracks.append(song)        def delete(self, number):                    del (self.song(number))            def look(self, song):          for song in self.tracks:              print(f"{song.artist} - {song.title}")        def duration(self):                s=0          for y in range(0,len(self.song)):             s = s + self.song[y]          print(int(datetime.timedelta(seconds=s)))      n = int(input("Enter the number of tracks to add"))  playlist = Play()    for i in range(n):      title, artist, duration = input("Enter the title, artist, and duration of a track").split(',')      song = Tune(title, artist, duration)      playlist.add(song)      playlist.look()  playlist.duration  playlist.delete (2)  

for every input, i have to make it an object pertaining to its class and add it to the list, with the option of removing it when it is called. When song.look is called, it is supposed to print out the title and artist in the list. For example, if the input is:

2  Artic Monkeys,R U Mine?,324  Artic Monkeys,Do I wanna know?,253  

when the program is executed it should output:

1 - Artic Monkeys - R U Mine?  2 - Artic Monkeys - Do I wanna know?  

when duration is executed:

00:09:62  

when the delete is executed:

1 - Artic Monkeys - R U Mine?  

and if song.duration is called, it should output the total duration in the format of hours:minutes:seconds. I think the add and look is correct, my only problem is how do i make it so that it can have track number and how can i use it to remove said tracks? and is my program for duration correct?

https://stackoverflow.com/questions/66450908/how-to-add-new-variables-into-objects-to-use-in-classes March 03, 2021 at 12:42PM

没有评论:

发表评论