0

So I have list of objects in class and I need to print all different songs from one singer. So far I have this code and I am not sure is it correct

Also I need to use recursion method

def allmusic(listofobjects, name):
    nameandsurname=self.name
    if(listofobjects.name==nameandsurname):
        print(listofobjects.music)
        return(listofobjects(music[1:]))
    else:
        return(listofobjects(name[1:]))

Also I need to print number of singers in that class and code I have is

def allmusic(listofobjects):
    numberofsingers=0
    for s in listofobjects:
       numberofsingers+=1
       return(listofobjects()[1:])
    print(numberofsingers)
5
  • 1
    What is self supposed to refer to in the first attempt? What do the elements of the input list look like? Commented Jun 26, 2020 at 13:16
  • So i have init method class Music def __init__(self,music,year,howlong,typeofmusic,nameofsinger): self.music=music self.year=year self.howlong=howlong self.typeofmusic=typeofmusic self.nameofsinger=nameofsinger Commented Jun 26, 2020 at 13:21
  • You need to add self in every function argument inside the class. Commented Jun 26, 2020 at 13:28
  • So i think when you create def method its already in class and you dont need to type everytime self Commented Jun 26, 2020 at 13:30
  • Refer to this link. google.com/amp/s/www.geeksforgeeks.org/self-in-python-class/amp Commented Jun 26, 2020 at 13:37

1 Answer 1

1

I think this is what you want. It recursively prints the all songs of a specific singer provided in the arguments. Here listOfMusic is the list of Music objects.

def PrintSinger(self, listOfMusic, name):
        if(listOfMusic[0].nameOfSinger == name):
            print(listOfMusic[0].music)
        if(len(listOfMusic) ==  1):
            return
        else:
            return self.PrintSinger(listOfMusic[1:], name)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.