Finding the new/unique listeners of a song


#Creating a class with two parameters

class Song:

  def __init__ (self,title,artist):

    self.title  = title

    self.artist = artist

  @property

  def how_many(self,people = []):

#The instance variable list is equal to list of people

    self.people = people

#count the number of unique people in the people list

#convert list to a set

    set_people = set(people)

    count = len(set_people)

    return count

    

song1 = Song("Cheap thrills", "Sia")

print(song1.how_many(["John","JOhn","Peter","Gabby","Ajmeher","Yuvi"]))

So my goal is to pass a list and find the new listeners of a song and return their number

I am getting the error below

TypeError: ‘int’ object is not callable

Thanks

It might work if you remove @property,

if you use @property, then you’ll have to write that print statement differently

why was @ property a problem?
After that l improved the code a bit and list_people2 is not updating each time I add new listeners, it like starts from afresh and doesn’t keep the collection of all the unique listeners l wish it to keep


#Creating a class with two parameters

class Song:

  def __init__ (self,title,artist):

    self.title  = title

    self.artist = artist

  

  def how_many(self,people = []):

#The instance variable list is equal to list of people

    

    self.people = people

    for items in range(len(people)):

      people[items] = people[items].lower()

    

#count the number of unique people in the people list

#convert list to a set

# count variable to keep track of new incremental people

    count = 0

#convert the people list to a set to get unique people in a day 1

    set_people = set(people)

#count number of people in first day

    day_1_listeners = len(set_people)

#convert to list to keep track of every unique listeners

    list_people = list(set_people)

# list_people2 to keep track of total unique listeners

    list_people2 = []

#To count incremental listeners

    for people in list_people:

      if people not in list_people2:

        list_people2.append(people)

        count +=1

    print("the collection of total unique listeners are: ", list_people2)

    return f'the day_1 listeners are {day_1_listeners}, the new incremental listeners from day 2 afterwards are {count}'

    

song1 = Song("Cheap thrills", "Sia")

print(song1.how_many(["John","JOhn","Peter","Gabby","Ajmeher","Yuvi"]))

print(song1.how_many(["John","Peter","Sam","Sibley","Kohli"]))

Thanks

if you want to add to .people each time,
you’d probably have to put
self.people += people

but you’d have to somehow deal with the case where there isn’t a self.people to you can add to yet
one way to deal with that is to put self.people = [] in the constructor (meaning inside the __init__ method)

alternatively, you could check if self.people does not exist yet,
since you’re checking for an attribute of an object, you could do:

if not hasattr(self, 'people'):
   self.people = []

before using self.people

also, note that the people = [] in def how_many(self,people = []): might act in unusual ways
here’s a link:
https://www.codecademy.com/courses/learn-intermediate-python-3/articles/python-gotcha-mutable-default-arguments

This is from the article link above

What is this highlighted bit the actual list is just called

grade = []

this is the code l managed to come up with though l didn’t use the none as a default argument to the parameter but used the concept/problem that items are appended to the list


#Creating a class with two parameters

class Song:

  def __init__ (self,title,artist,people):

    self.title  = title

    self.artist = artist

    self.people = people

  

  def how_many(self,people = []):

#The instance variable is added on each time

    

    self.people += people

    for items in range(len(people)):

      people[items] = people[items].lower()

    

#count the number of unique people in the people list

#convert list to a set

#convert the people list to a set to get unique people in a day 1

    set_people = set(people)

#Find length of set_people 

    len1= len(set_people)

# list to store the length of people at each time

    lst_3 = []

#Appended to find the difference

    lst_3.append(len1)

# To find difference when new listeners are added

    new = lst_3[1] - lst_3[0]

    print(f'the initial/incremental  listeners are {new}')

# remove the number at index 0 to have room for another number/additional length of people  

    if len(lst_3) == 2:

      lst_3.remove[0]

       

song1 = Song("Cheap thrills", "Sia",["John","JOhn","Peter","Gabby","Ajmeher","Yuvi"])

print(song1.how_many(["John","Peter","Sam","Sibley","Kohli"]))

Can someone please help on this

Thank you

I don’t completely understand what you’re trying to do with lst_3 in the function how_many,
but

My best guess is:
that you’re using it to store the old and new lengths of self.people so that you can get the number of people added

replace
len1= len(set_people)
with
len1 = len(self.people)
and instead of having
lst_3 = []
put
lst_3 = [ len(self.people) ]
at the beginning of the function

alternatively, you could have

new = len(people)
if new == 0:
  new = len(self.people)

also, you probably want the function how_many to return something:
so at the end of the function, put

return len(self.people)

here’s some of the stuff above as code:

#Creating a class with two parameters class Song: def __init__ (self,title,artist,people): self.title = title self.artist = artist self.people = people def how_many(self,people = []): # list to store the length of people at each time lst_3 = [len(self.people)] #The instance variable is added on each time self.people += people for items in range(len(people)): people[items] = people[items].lower() #count the number of unique people in the people list #convert list to a set #convert the people list to a set to get unique people in a day 1 set_people = set(people) #Find length of set_people #len1 = len(set_people) len1 = len(self.people) # list to store the length of people at each time #lst_3 = [] #Appended to find the difference lst_3.append(len1) # To find difference when new listeners are added new = lst_3[1] - lst_3[0] print(f'the initial/incremental listeners are {new}') # remove the number at index 0 to have room for another number/additional length of people #if len(lst_3) == 2: #lst_3.remove(lst_3[0]) return len(self.people) song1 = Song("Cheap thrills", "Sia",["John","JOhn","Peter","Gabby","Ajmeher","Yuvi"]) print(song1.how_many(["John","Peter","Sam","Sibley","Kohli"]))

I hope that helps.

If you only want unique people added to the list (and upper or lowercase does not matter),
then you could use that set stuff (that you already have) in more places …

  def __init__ (self,title,artist,people):

    self.title  = title

    self.artist = artist

    for i in range(len(people)):
      people[i] = people[i].lower()
  
    self.people = list(set(people))

and something similar in the how_many function, except you’d have
self.people = list(set( self.people + people )) for that one

This first list is supposed to give us a total listeners 5, with the two John’s counted as 1 due to ignoring case sensitivity and uniqueness

This is supposed to give us 3, because John and Peter are already in the first, so the incremental listeners should be 3

The error l was getting from what l wrote is that list index is out of range

Thanks

the index is out of range error
comes from lst_3
(there is no lst_3[1] )
This happens because you forgot to put the original length of self.people into lst_3 before doing lst_3.append(len1)

note that I changed
self.people += people
to
self.people = list(set( self.people + people ))
to guarantee uniqueness

and I changed
lst_3.remove[0]
to
lst_3.pop(0)

here’s the same stuff mentioned in the earlier posts as a codebyte:

#Creating a class with two parameters class Song: def __init__ (self, title, artist, people=None): self.title = title self.artist = artist if people is None: people = [] for i in range(len(people)): people[i] = people[i].lower() self.people = list(set(people)) def how_many(self, people=None): if people is None: people = [] len0 = len(self.people) print(f'the initial listeners amount: {len0}') return len(self.people) # list to store the length of people at each time lst_3 = [ len(self.people) ] for items in range(len(people)): people[items] = people[items].lower() # list to store the length of people at each time lst_3 = [ len(self.people) ] #count the number of unique people in the people list len0 = len(self.people) #convert list to a set set_people = set(people) #Find length of set_people len1 = len(set_people) #added people to self.people in a way that #only have unique persons in self.people self.people = list(set( self.people + people )) #find new length len2 = len(self.people) #Appended to find the difference lst_3.append(len2) # To find difference when new listeners are added new = lst_3[1] - lst_3[0] print(f'the incremental listeners amount: {new}') # remove the number at index 0 to have room for another number/additional length of people if len(lst_3) == 2: lst_3.pop(0) return len(self.people) song1 = Song("Cheap thrills", "Sia",["John","JOhn","Peter","Gabby","Ajmeher","Yuvi"]) print(song1.how_many()) print(song1.how_many(["John","Peter","Sam","Sibley","Kohli"]))
1 Like