1

Ok so I have been hunting around for awhile, please excuse the fact that this is probably a fairly simple question, but no matter how many threads I read of the same error type, I can't figure out what I am doing wrong. I am missing something conceptually I believe.

Here is the code:

class Song(object): def __init__ (self, lyrics, feliz): self.lyrics = lyrics self.feliz = "feliz navidad, etc etc" def sing_me_a_song(self): for line in self.lyrics: print line happy_bday = Song(["\n\nHappy birthday to you", "I don't want to get sued", "So I'll stop right there.\n\n\n"]) bulls_on_parade = Song(["They rally around the family", "With pockets full of shells.\n"]) happy_bday.sing_me_a_song() bulls_on_parade.sing_me_a_song() songz = Song() print songz.feliz() 

Here is the error I get:

Traceback (most recent call last): File "ex40.py", line 13, in <module> "So I'll stop right there.\n\n\n"]) TypeError: __init__() takes exactly 3 arguments (2 given) 

I realize this is probably dumb, but I am hung up on this and would really like some help. If it means anything this is obviously from LPTHW and I have editted lesson 40, the code words fine if i remove all the references to feliz, that was me trying to add my own variable to the class Song and then have it print out at the end. Thank you in advance.


So I can make it do what I want by adding the following:

feliz_navidad = Song(["Feliz navidad", "Feliz navidad", "Prospero anos delicidad.\n"]) feliz_navidad.sing_me_a_song() 

But, is there no other way for me to use the class to have it print a string than to use the defined sing_me_a_song function?

I was attempting to reference the Song class and have it print out a string without using a function defined inside... I suppose that is a dumb thing to do. I misunderstood how it works...

1
  • Your __init__ method asks for two arguments (self is passed implicitly), but you only give it one. You never use feliz. Commented Jan 22, 2014 at 3:45

2 Answers 2

1

You defined your __init__ function to accept two arguments, lyrics and feliz. You only passed one argument, which appears to be lyrics.

It's not clear from your question what you want your code to do. If you only want to pass one argument, just remove feliz from the argument list of your __init__, so it's just def __init__(self, lyrics). Right now, you aren't using the feliz argument at all, so it doesn't seem to have any purpose.

I also suggest you read the Python tutorial to get a grasp of the basics of Python.

Sign up to request clarification or add additional context in comments.

Comments

0

Here

happy_bday = Song(["\n\nHappy birthday to you", "I don't want to get sued", "So I'll stop right there.\n\n\n"]) 

You are passing a list[...], that is 1 paramter. The exception says 2 given, because self is passed implictly as parameter. But the __init__ method you declared expects 3:

  • self
  • lyrics
  • feliz

I would recommend you to delete feliz parameter, because don't matter what you pass as argument, you are just assigning feliz a string "feliz navidad, etc etc"

def __init__ (self, lyrics): 

1 Comment

Lyrics is intended to be a list. Look at his code for sing_me_a_song

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.