maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Learning Python and I need help (https://talk.maemo.org/showthread.php?t=88113)

bandora 2012-12-04 01:48

Learning Python and I need help
 
Hey,

So I am taking a basic Python class and one of my assignments was to create a very simple calculator..

Btw, I am using Python 2.3 (As that's what the teacher wants us to use because of livewires)

It's very simple, but I am experiencing an error that I don't know how to fix (which is probably too simple for you guys to fix haha).. So I am getting this error on IDLE:

Quote:

Traceback (most recent call last):
File "C:/Python23/calculator", line 38, in -toplevel-
a.retry()
File "C:/Python23/calculator", line 32, in retry
Calculator.calc()
TypeError: unbound method calc() must be called with Calculator instance as first argument (got nothing instead)

I don't know how to fix it, I've tried multiple things and I couldn't get it to work.. Here's the code:

Code:

import sys

class Calculator(object):
    def calc(self):
        x = int(input("Please enter the first number: "))
        y = int(input("Please enter the second number: "))
        #op = Operator
        op = str.lower(raw_input(
              "Please enter what method you want to use ((A)ddition, (S)ubtraction, (M)ultiplication, (D)ivision) or type 'end' to quit: "))
        if op != "a"  and op != "s" and op != "m" and op != "d" and op != "end":
            exit = str.lower(raw_input("Are you sure you want to exit calculator (Yes or No)? "))
            if exit == "yes":
                sys.exit(0)
            else:
                calc()
        else:
            if op == "a":
                print ("\nThe result is: ", x, "+", y, "=", str(x+y))
   
            elif op == "s":
                print ("\nThe result is: ", x, "-", y, "=", str(x-y))

            elif op == "m":
                print ("\nThe result is: ", x, "*", y, "=", str(x*y))

            elif op == "d":
                print ("\nThe result is: ", x, "/", y, "=", str(float(x)/float(y)))

    def retry(self):
        confirm = str.lower(raw_input("Would you like to do another calculation (Yes or No)? "))
        if confirm == "yes":
            Calculator.calc()
        else:
            sys.exit(0)

a = Calculator()
a.calc()
a.retry()


raw_input("\nPress any key to exit.")

If you guys can help me on this one that would be great.. I am just curious on how to fix this small issue.

bandora 2012-12-04 01:58

Re: Learning Python and I need help
 
Okay fixed the first error:

But I am getting another error if I choose "No" when it asks me to do another calculation:

Quote:

Traceback (most recent call last):
File "C:/Python23/calculator", line 41, in -toplevel-
a.retry()
File "C:/Python23/calculator", line 37, in retry
sys.exit(0)
SystemExit: 0

Here's the new code (I put what I changed in bold):

Code:

import sys



class Calculator(object):
    def calc(self):
        x = int(input("Please enter the first number: "))
        y = int(input("Please enter the second number: "))
        #op = Operator
        op = str.lower(raw_input(
            "Please enter what method you want to use ((A)ddition, (S)ubtraction, (M)ultiplication, (D)ivision) or type 'end' to quit: "))
        if op != "a"  and op != "s" and op != "m" and op != "d" and op != "end":
            exit = str.lower(raw_input("Are you sure you want to exit calculator (Yes or No)? "))
            if exit == "yes":
                sys.exit(0)
            else:
                calc()
        else:
            if op == "a":
                print ("\nThe result is: ", x, "+", y, "=", str(x+y))
   
            elif op == "s":
                print ("\nThe result is: ", x, "-", y, "=", str(x-y))

            elif op == "m":
                print ("\nThe result is: ", x, "*", y, "=", str(x*y))

            elif op == "d":
                print ("\nThe result is: ", x, "/", y, "=", str(float(x)/float(y)))

    def retry(self):
        confirm = str.lower(raw_input("Would you like to do another calculation (Yes or No)? "))
        b = Calculator()
        if confirm == "yes":
            b.calc()

        else:
            sys.exit(0)

a = Calculator()
a.calc()
a.retry()


raw_input("\nPress any key to exit.")


marxian 2012-12-04 02:03

Re: Learning Python and I need help
 
In the retry() method,

Code:

Calculator.calc()
should be

Code:

self.calc()
EDIT: No need to create another instance of Calculator, as the existing instance is passed as the argument to both calc() and retry().

bandora 2012-12-04 06:23

Re: Learning Python and I need help
 
Thanks marxian!

Ah good stuff! I changed the code quite a bit and now it's working like how I want it! :)

Code:

import sys

class Calculator(object):
    def quit_prompt(self):
            exit = str.lower(raw_input("Are you sure you want to exit calculator (Yes or No)? "))
            if exit == "yes":
                sys.exit()
            else:
                calc()

    def add(self, x, y):
        print "The result is: ", x, "+", y, "=", str(x+y)
    def subt(self, x, y):
        print "The result is: ", x, "-", y, "=", str(x-y)
    def mult(self, x, y):
        print "The result is: ", x, "*", y, "=", str(x*y)
    def div(self, x, y):
        print "The result is: ", x, "/", y, "=", str(float(x)/float(y))
       
    def calc(self):
        x = int(input("Please enter the first number: "))
        y = int(input("Please enter the second number: "))
        #op = Operator
        op = str.lower(raw_input(
            "Please enter what method you want to use ((A)ddition, (S)ubtraction, (M)ultiplication, (D)ivision) or type 'end' to quit: ")) 
        while op != "end":
            if op == "a":
                self.add(x, y)
   
            if op == "s":
                self.subt(x, y)

            if op == "m":
                self.mult(x, y)
   
            if op == "d":
                self.div(x, y)
            self.retry()
            break
       

    def retry(self):
        confirm = str.lower(raw_input("\nWould you like to do another calculation (Yes or No)? "))
        if confirm == "yes":
            self.calc()
        else:
            pass

a = Calculator()
a.calc()

print "\nThank you for using the calculator!"
raw_input("\nPress any key to exit.")


bandora 2012-12-16 02:39

Re: Learning Python and I need help
 
EDITED: Nvm, I solved the new issue by myself! Props to me! lol

udaychaitanya16 2012-12-16 08:07

Re: Learning Python and I need help
 
Thats great.

Dave999 2012-12-16 09:38

Re: Learning Python and I need help
 
Great bandora. Python rules. Why not adjust it and add a gui and release it for your favorite device? Or are you unly looking to learn python syntax?

bandora 2012-12-16 18:11

Re: Learning Python and I need help
 
Well I am taking a Python class, so all this is pretty much just for learning purposes.. And the edited post was about another code.. I am making a simple python game using pygame and livewires (it's my final), so far so good lol...

I absolutely love TMO on this matter! Like it's pretty much guaranteed you're gonna get helped developing anything! Thanks again! :)


All times are GMT. The time now is 07:16.

vBulletin® Version 3.8.8