Reply
Thread Tools
bandora's Avatar
Posts: 1,302 | Thanked: 936 times | Joined on Oct 2009 @ Michigan, USA / Jordan
#1
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:

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.
__________________
FarahFa.com • Qayyem.com

Last edited by bandora; 2012-12-04 at 02:01.
 
bandora's Avatar
Posts: 1,302 | Thanked: 936 times | Joined on Oct 2009 @ Michigan, USA / Jordan
#2
Okay fixed the first error:

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

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.")
__________________
FarahFa.com • Qayyem.com
 
marxian's Avatar
Posts: 2,358 | Thanked: 8,746 times | Joined on Aug 2010 @ Wigan, UK
#3
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().
__________________
“It’s already dog-eat-dog, friend. I’m not sure what worse a bunch of zombies could do.”

My website

GitHub
 

The Following 2 Users Say Thank You to marxian For This Useful Post:
bandora's Avatar
Posts: 1,302 | Thanked: 936 times | Joined on Oct 2009 @ Michigan, USA / Jordan
#4
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.")
__________________
FarahFa.com • Qayyem.com

Last edited by bandora; 2012-12-04 at 07:45. Reason: Fixed all the errors yay me! :) Thanks marxian!
 
bandora's Avatar
Posts: 1,302 | Thanked: 936 times | Joined on Oct 2009 @ Michigan, USA / Jordan
#5
EDITED: Nvm, I solved the new issue by myself! Props to me! lol
__________________
FarahFa.com • Qayyem.com

Last edited by bandora; 2012-12-16 at 03:04.
 

The Following User Says Thank You to bandora For This Useful Post:
Posts: 212 | Thanked: 59 times | Joined on May 2010 @ India
#6
Thats great.
 

The Following User Says Thank You to udaychaitanya16 For This Useful Post:
Dave999's Avatar
Posts: 4,534 | Thanked: 3,611 times | Joined on Oct 2009 @ Moon! It's not the East or the West side... it's the Dark Side
#7
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?
__________________
I'm only responsible for what I say, not for what you understand.
Live Spy Cam Jolla Party <-- Live unboxing at 3:29:00
N9 Dance
I Don't - n900

Hitler Finds Out Nokia's Ovi Maps Turn By Turn navigation is now free

The force is strong with this one
All Trolls, please rise for the anthem...RIP
I'm living on the EDGE! THE UBUNTU EDGE!<--Click Here!
I'm also a serial thread derailer!

Last edited by Dave999; 2012-12-16 at 19:17.
 

The Following User Says Thank You to Dave999 For This Useful Post:
bandora's Avatar
Posts: 1,302 | Thanked: 936 times | Joined on Oct 2009 @ Michigan, USA / Jordan
#8
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!
__________________
FarahFa.com • Qayyem.com
 
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 12:25.