Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-10-2012, 06:52 PM
Chuck
Guest
 
Posts: n/a
Default Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (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!
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-10-2012, 07:05 PM
Pedro Kroger
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (0 given)"


On Aug 10, 2012, at 3:52 PM, Chuck <galois271@gmail.com> wrote:

> if __name__ == '__main__':
>
> quiz()
>
>


You need to instantiate your class:

foo = ElementsQuiz()
foo.quiz()


Pedro
-----
http://pedrokroger.net
http://musicforgeeksandnerds.com

Reply With Quote
  #3 (permalink)  
Old 08-10-2012, 07:23 PM
Chuck
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (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 <galois271@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"
Reply With Quote
  #4 (permalink)  
Old 08-10-2012, 07:23 PM
Chuck
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (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 <galois271@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"
Reply With Quote
  #5 (permalink)  
Old 08-10-2012, 08:28 PM
Chuck
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (0 given)"

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!
Reply With Quote
  #6 (permalink)  
Old 08-10-2012, 08:28 PM
Chuck
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (0 given)"

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!
Reply With Quote
  #7 (permalink)  
Old 08-10-2012, 08:50 PM
Dave Angel
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (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
Reply With Quote
  #8 (permalink)  
Old 08-10-2012, 08:54 PM
Chuck
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (0 given)"

Yeah, I am mostly a Java guy. Just starting with Python.
Reply With Quote
  #9 (permalink)  
Old 08-10-2012, 08:54 PM
Chuck
Guest
 
Posts: n/a
Default Re: Keep getting this in PyDev "TypeError: quiz() takes exactly 1argument (0 given)"

Yeah, I am mostly a Java guy. Just starting with Python.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:42 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.