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

Last edited by bandora; 2012-12-04 at 07:45. Reason: Fixed all the errors yay me! :) Thanks marxian!