Please help me understand where im going wrong with classes

A forum for general discussion of the Python programming language.

Please help me understand where im going wrong with classes

Postby buttaflyman » Wed Jun 12, 2013 7:38 pm

can anyone please help im a beginner and i dont know what the problem is with this code:

Code: Select all
class main():
    def mainmethod():
        answer = 0
        thingchosen = raw_input("add or minus: ")
        num1 = input("number 1: ")
        num2 = input("number 2: ")
        add = add()
        minus = minus()
        add.addmethod()
        minus.minusmethod()
        print "the an swer is " + str(answer)
class add(main):
    def addmethod(self):
        main = main()
        if (main.thingchosen == "add"):
            main.answer = main.num1 + main.num2
class minus(main):
    def minusmethod(self):
        main = main()
        if (main.thingchosen == "minus"):
            main.answer = main.num1 + main.num2
class benji(main):
    main = main()
    main.mainmethod()


when i run it it says this:
Traceback (most recent call last):
File "C:/Users/benji/Desktop/Python/Python scripts/benji.py", line 22, in <module>
class benji(main):
File "C:/Users/benji/Desktop/Python/Python scripts/benji.py", line 24, in benji
balla.mainmethod()
TypeError: mainmethod() takes no arguments (1 given)

any help would be appreciated
Last edited by Yoriz on Wed Jun 12, 2013 10:52 pm, edited 1 time in total.
Reason: Editted the title to include more information.
buttaflyman
 
Posts: 4
Joined: Wed Jun 12, 2013 7:31 pm

Re: Can anyone please help

Postby Yoriz » Wed Jun 12, 2013 7:47 pm

In class Main
Code: Select all
def mainmethod():

should have self as its first argument.
Code: Select all
def mainmethod(self):
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Can anyone please help

Postby buttaflyman » Wed Jun 12, 2013 8:03 pm

i changed it to:

Code: Select all
class main():
    def mainmethod(self):
        answer = 0
        thingchosen = raw_input("add or minus: ")
        num1 = input("number 1: ")
        num2 = input("number 2: ")
        add = add()
        minus = minus()
        add.addmethod()
        minus.minusmethod()
        print "the an swer is " + str(answer)
class add():
    def addmethod(self):
        main = main()
        if (main.thingchosen == "add"):
            main.answer = main.num1 + main.num2
class minus():
    def minusmethod(self):
        main = main()
        if (main.thingchosen == "minus"):
            main.answer = main.num1 + main.num2
class benji():
    main = main()
    main.mainmethod()


and now the error comes after the part where the computer asks me what number 2 should be:

Traceback (most recent call last):
File "C:/Users/benji/Desktop/Python/Python scripts/benji.py", line 22, in <module>
class benji():
File "C:/Users/benji/Desktop/Python/Python scripts/benji.py", line 24, in benji
main.mainmethod()
File "C:/Users/benji/Desktop/Python/Python scripts/benji.py", line 7, in mainmethod
add = add()
UnboundLocalError: local variable 'add' referenced before assignment
buttaflyman
 
Posts: 4
Joined: Wed Jun 12, 2013 7:31 pm

Re: Can anyone please help

Postby Yoriz » Wed Jun 12, 2013 8:16 pm

You need to learn how to use classes, or you are just going to get error after error.
Do some class tutorials like http://www.python-forum.org/viewtopic.php?f=25&t=361
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Can anyone please help

Postby PurityLake » Wed Jun 12, 2013 8:17 pm

Now That is some pretty badly written code I must admit. It is easier to pass parameters than to pass class references which at the time of making the main class there is no other classes made. stick to parameters or else define all other important classes before main
PurityLake
 
Posts: 7
Joined: Wed Jun 12, 2013 5:26 pm

Re: Please help me understand where im going wrong with clas

Postby micseydel » Thu Jun 13, 2013 12:08 am

What you're doing does not require object-orientation, and in fact should not use it (that means no classes in a program like this).

It looks like you've come from a Java background. Amateur programmers who start with Java and come to Python are brainf*cked. The program you're trying to write can be written without even any indentation. KISS. Also, you should never have anything of this form
Code: Select all
variable = variable()

Because you're destroying access to the thing on the right hand side.

I'd like to give you advice with regard to classes as well, but am going to not until you show a decent grasp of (1) proper simpler programming and (2) a good thing to actually use OOP on. Classes are advanced, and things done in Java are really silly and other languages don't do it that way because that way is bad even though it's the only way to get things done in Java.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1256
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Please help me understand where im going wrong with clas

Postby ochichinyezaboombwa » Thu Jun 13, 2013 5:45 am

micseydel wrote:It looks like you've come from a Java background. Amateur programmers who start with Java and come to Python are brainf*cked.
-- harsh but true.
Code: Select all
class main():
    def mainmethod():
is really funny: doing nothing, telling nothing, but occupying two lines.

That's not your fault of course; your teachers were also brainf*cked, for historical reasons(...). So just start over.

Remember that things like "add", "minus" are actually verbs and not classes. I recommend you go through a few first chapters of a Python tutorial (plentiful available online) and forget Java. You'd be glad when one day you only write 2-3 clear lines of code instead of 50-100 "plumbing" ones.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Please help me understand where im going wrong with clas

Postby ochichinyezaboombwa » Thu Jun 13, 2013 5:59 am

Thinking a bit more of this: we ALL are brainf*cked.
The very word "main" – in C, C++, ... Java, ... Python, -- is just pure wrong. It should from the very beginning be "do", "do_stuff", "do_what_I_mean".

Does anybody agree?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests