View Single Post
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, 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

Last edited by bandora; 2012-12-04 at 02:01.