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