Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
Hi all, I cannot figure why I keep getting this error. It is my understanding that all methods need a self argument when designing a class. Here is my code: import random class ElementsQuiz: elements = {'H' : 'Hydrogen', 'He' : 'Helium', 'Li' : 'Lithium', 'Be' : 'Beryllium', 'B' : 'Boron', 'C' : 'Carbon', 'N' : 'Nitrogen', 'O' : 'Oxygen', 'F' : 'Fluorine', 'Ne' : 'Neon', 'Na' : 'Sodium', 'Mg' : 'Magnesium', 'Al' : 'Aluminium', 'Si' : 'Silicon', 'P' : 'Phosphorus', 'S' : 'Sulfur', 'Cl' : 'Chlorine', 'Ar' : 'Argon', 'K' : 'Potassium', 'Ca' : 'Calcium', 'Sc' : 'Scandium', 'Ti' : 'Titanium', 'V' : 'Vanadium', 'Cr' : 'Chromium', 'Mn' : 'Manganese', 'Fe' : 'Iron', 'Co' : 'Cobalt', 'Ni' : 'Nickel', 'Cu' : 'Copper', 'Zn' : 'Zinc', 'Ga' : 'Gallium', 'Ge' : 'Germanium', 'As' : 'Arsenic', 'Se' : 'Selenium', 'Br' : 'Bromine', 'Kr' : 'Krypton', 'Rb' : 'Rubidium', 'Sr' : 'Strontium', 'Y' : 'Yttrium', 'Zr' : 'Zirconium', 'Nb' : 'Niobium', 'Mo' : 'Molybdenum', 'Tc' : 'Technetium', 'Ru' : 'Ruthenium', 'Rh' : 'Rhodium', 'Pd' : 'Palladium', 'Ag' : 'Silver', 'Cd' : 'Cadmium', 'In' : 'Indium', 'Sn' : 'Tin', 'Sb' : 'Antimony', 'Te' : 'Tellurium', 'I' : 'Iodine', 'Xe' : 'Xenon', 'Cs' : 'Caesium', 'Ba' : 'Barium', 'La' : 'Lanthanum', 'Ce' : 'Cerium', 'Pr' : 'Praseodymium', 'Nd' : 'Neodymium', 'Pm' : 'Promethium', 'Sm' : 'Samarium', 'Eu' : 'Europium', 'Gd' : 'Gadolinium', 'Tb' : 'Terbium', 'Dy' : 'Dysprosium', 'Ho' : 'Holmium', 'Er' : 'Erbium', 'Tm' : 'Thulium', 'Yb' : 'Ytterbium', 'Lu' : 'Lutetium', 'Hf' : 'Hafnium', 'Ta' : 'Tantalum', 'W' : 'Tungsten', 'Re' : 'Rhenium', 'Os' : 'Osmium', 'Ir' : 'Iridium', 'Pt' : 'Platinum', 'Au' : 'Gold', 'Hg' : 'Mercury', 'Tl' : 'Thallium', 'Pb' : 'Lead', 'Bi' : 'Bismuth', 'Po' : 'Polonium', 'At' : 'Astatine', 'Rn' : 'Radon', 'Fr' : 'Francium', 'Ra' : 'Radium', 'Ac' : 'Actinium', 'Th' : 'Thorium', 'Pa' : 'Protactinium', 'U' : 'Uranium', 'Np' : 'Neptunium', 'Pu' : 'Plutonium', 'Am' : 'Americium', 'Cm' : 'Curium', 'Bk' : 'Berkelium', 'Cf' : 'Californium', 'Es' : 'Einsteinium', 'Fm' : 'Fermium', 'Md' : 'Mendelevium', 'No' : 'Nobelium', 'Lr' : 'Lawrencium', 'Rf' : 'Rutherfordium', 'Db' : 'Dubnium', 'Sg' : 'Seaborgium', 'Bh' : 'Bohrium', 'Hs' : 'Hassium', 'Mt' : 'Meitnerium', 'Ds' : 'Darmstadtium', 'Rg' : 'Roentgenium', 'Cn' : 'Copernicium', 'Uut' : 'Ununtrium', 'Fl' : 'Flerovium', 'Uup' : 'Ununpentium', 'Lv' : 'Livermorium', 'Uus' : 'Ununseptium', 'Uuo' : 'Ununoctium' } def __init__(self): self.quiz() def quiz(self): self.reply = ('Moron', 'Dummy', 'Idiot', 'Embecile', 'Half-wit') self.numCorrect = 0 self.question = random.choice(self.elements.keys()) print self.question self.ans = raw_input('Answer: ') if self.ans == self.elements(self.question): self.numCorrect += 1 else: self.insult = random.choice(self.reply) print 'Incorrect %s' % self.insult if __name__ == '__main__': quiz() Thanks for any help! --
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
On Aug 10, 2012, at 3:52 PM, Chuck galois...@gmail.com wrote: if __name__ == '__main__': quiz() You need to instantiate your class: foo = ElementsQuiz() foo.quiz() Pedro - http://pedrokroger.net http://musicforgeeksandnerds.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
On Friday, August 10, 2012 2:05:36 PM UTC-5, Pedro Kroger wrote: On Aug 10, 2012, at 3:52 PM, Chuck galois...@gmail.com wrote: if __name__ == '__main__': quiz() You need to instantiate your class: foo = ElementsQuiz() foo.quiz() Pedro - http://pedrokroger.net http://musicforgeeksandnerds.com That doesn't work either for some reason. I keep getting NameError: name 'ElementsQuiz' is not defined -- http://mail.python.org/mailman/listinfo/python-list
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
On 08/10/2012 02:52 PM, Chuck wrote: Hi all, I cannot figure why I keep getting this error. It is my understanding that all methods need a self argument when designing a class. Here is my code: It'd be much more useful if you'd actually quote the entire error. import random class ElementsQuiz: elements = {'H' : 'Hydrogen', 'He' : 'Helium', 'Li' : 'Lithium', 'Be' : 'Beryllium', 'B' : 'Boron', 'C' : 'Carbon', 'N' : 'Nitrogen', 'O' : 'Oxygen', 'F' : 'Fluorine', 'Ne' : 'Neon', 'Na' : 'Sodium', 'Mg' : 'Magnesium', 'Al' : 'Aluminium', 'Si' : 'Silicon', 'P' : 'Phosphorus', 'S' : 'Sulfur', 'Cl' : 'Chlorine', 'Ar' : 'Argon', 'K' : 'Potassium', 'Ca' : 'Calcium', 'Sc' : 'Scandium', 'Ti' : 'Titanium', 'V' : 'Vanadium', 'Cr' : 'Chromium', 'Mn' : 'Manganese', 'Fe' : 'Iron', 'Co' : 'Cobalt', 'Ni' : 'Nickel', 'Cu' : 'Copper', 'Zn' : 'Zinc', 'Ga' : 'Gallium', 'Ge' : 'Germanium', 'As' : 'Arsenic', 'Se' : 'Selenium', 'Br' : 'Bromine', 'Kr' : 'Krypton', 'Rb' : 'Rubidium', 'Sr' : 'Strontium', 'Y' : 'Yttrium', 'Zr' : 'Zirconium', 'Nb' : 'Niobium', 'Mo' : 'Molybdenum', 'Tc' : 'Technetium', 'Ru' : 'Ruthenium', 'Rh' : 'Rhodium', 'Pd' : 'Palladium', 'Ag' : 'Silver', 'Cd' : 'Cadmium', 'In' : 'Indium', 'Sn' : 'Tin', 'Sb' : 'Antimony', 'Te' : 'Tellurium', 'I' : 'Iodine', 'Xe' : 'Xenon', 'Cs' : 'Caesium', 'Ba' : 'Barium', 'La' : 'Lanthanum', 'Ce' : 'Cerium', 'Pr' : 'Praseodymium', 'Nd' : 'Neodymium', 'Pm' : 'Promethium', 'Sm' : 'Samarium', 'Eu' : 'Europium', 'Gd' : 'Gadolinium', 'Tb' : 'Terbium', 'Dy' : 'Dysprosium', 'Ho' : 'Holmium', 'Er' : 'Erbium', 'Tm' : 'Thulium', 'Yb' : 'Ytterbium', 'Lu' : 'Lutetium', 'Hf' : 'Hafnium', 'Ta' : 'Tantalum', 'W' : 'Tungsten', 'Re' : 'Rhenium', 'Os' : 'Osmium', 'Ir' : 'Iridium', 'Pt' : 'Platinum', 'Au' : 'Gold', 'Hg' : 'Mercury', 'Tl' : 'Thallium', 'Pb' : 'Lead', 'Bi' : 'Bismuth', 'Po' : 'Polonium', 'At' : 'Astatine', 'Rn' : 'Radon', 'Fr' : 'Francium', 'Ra' : 'Radium', 'Ac' : 'Actinium', 'Th' : 'Thorium', 'Pa' : 'Protactinium', 'U' : 'Uranium', 'Np' : 'Neptunium', 'Pu' : 'Plutonium', 'Am' : 'Americium', 'Cm' : 'Curium', 'Bk' : 'Berkelium', 'Cf' : 'Californium', 'Es' : 'Einsteinium', 'Fm' : 'Fermium', 'Md' : 'Mendelevium', 'No' : 'Nobelium', 'Lr' : 'Lawrencium', 'Rf' : 'Rutherfordium', 'Db' : 'Dubnium', 'Sg' : 'Seaborgium', 'Bh' : 'Bohrium', 'Hs' : 'Hassium', 'Mt' : 'Meitnerium', 'Ds' : 'Darmstadtium', 'Rg' : 'Roentgenium', 'Cn' : 'Copernicium', 'Uut' : 'Ununtrium', 'Fl' : 'Flerovium', 'Uup' : 'Ununpentium', 'Lv' : 'Livermorium', 'Uus' : 'Ununseptium', 'Uuo' : 'Ununoctium' } def __init__(self): self.quiz() Why would you do all the work from a call inside the initializer? What's the point of having instance attributes if you're not going to use it a second time? def quiz(self): self.reply = ('Moron', 'Dummy', 'Idiot', 'Embecile', 'Half-wit') self.numCorrect = 0 self.question = random.choice(self.elements.keys()) print self.question
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
On 8/10/2012 2:52 PM, Chuck wrote: Hi all, I cannot figure why I keep getting this error. To supplement Dave's answer (post entire traceback, dedent last two lines), here are the essentials of your code that show the problem. class C: def f(self): pass f() Traceback (most recent call last): File pyshell#17, line 1, in module class C: File pyshell#17, line 3, in C f() TypeError: f() missing 1 required positional argument: 'self' You problem is that you are calling the function during the execution of the class statement, *before* the class is created. Rather unusual ;-). -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
Thanks for the help guys! I finally got it working. Shouldn't I technically call quiz() through the constructor, though? Otherwise, the constructor is pointless. I just put in pass for now. (Also, I always thought that if __name__ == '__main__': went IN the class. Why wouldn't it be apart of the class? ) Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
On 08/10/2012 04:28 PM, Chuck wrote: Thanks for the help guys! I finally got it working. Shouldn't I technically call quiz() through the constructor, though? Otherwise, the constructor is pointless. Thanks again! What language did you use before trying Python? Was it java, by any chance, where everything has to be in a class? If you're going to do everything from the constructor, then why on earth would you make it a class? (Incidentally, __init__() is the initializer, not the constructor, which is called __new__() ) If you want to make a proper class out of it, you'd move the initalization code into the __init__(), call, the stuff that only needs to be called once per instance. On the other hand, you have a class attribute 'elements, which gets initialized outside of any mmthod. And everything else is just locals. I just put in pass for now. (Also, I always thought that if __name__ == '__main__': went IN the class. Why wouldn't it be apart of the class? ) Seems like you're arguing both sides. Anyway, the if __name__ stuff does not belong inside any class. This is Python. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
Yeah, I am mostly a Java guy. Just starting with Python. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Keep getting this in PyDev TypeError: quiz() takes exactly 1 argument (0 given)
On 08/10/2012 01:28 PM, Chuck wrote: Thanks for the help guys! I finally got it working. Shouldn't I technically call quiz() through the constructor, though? Otherwise, the constructor is pointless. I just put in pass for now. For this particular example, frankly, a class doesn't make sense. Just write it as a set of independent functions. A class would make more sense if you wanted to make this a generic Quiz class, then you could change the actual quiz simply by passing an appropriate dictionary to it when it's instantiated. But even that could be done with simple functions as well. (Also, I always thought that if __name__ == '__main__': went IN the class. Why wouldn't it be apart of the class? ) No way! That's nonsense also. You need to go through some introductory tutorials. Just keep in mind that Python and Java are VERY different languages. (I don't know Java myself, my (hobby) programming background has been with C, and I'm still just starting to learn Python, too.) Thanks again! -=- Larry -=- PS. On another subject... You need to check your newsreader -- all your responses have been double-posted. -- http://mail.python.org/mailman/listinfo/python-list