Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (0 given)"

C

Chuck

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!
 
D

Dave Angel

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
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()
Any reason why you put those two lines inside the class? That's your
mainline code, and should be start at the left margin.

Once you move the if __name__ stuff to the margin, and instantiated an
object as Pedro mentions, and removed the call to quiz() from inside the
initializer, it might work.

I assume this is in a class because that was how it's assiged?
 
T

Terry Reedy

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.
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 ;-).
 
C

Chuck

Thanks for the help guys! I finally got it working. Shouldn't I technically call quiz() through the constructor, though? Otherwise, the constructoris 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!
 
C

Chuck

Thanks for the help guys! I finally got it working. Shouldn't I technically call quiz() through the constructor, though? Otherwise, the constructoris 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!
 
D

Dave Angel

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.
 

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top