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 > 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? -- DaveA -- http://mail.python.org/mailman/listinfo/python-list