|
|
12-03-2012
, 09:58 PM
|
|
|
Posts: 1,223 |
Thanked: 834 times |
Joined on Oct 2009
@ Michigan, USA / Jordan
|
#2
|
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
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.")
|
|
12-03-2012
, 10:03 PM
|
|
|
Posts: 2,131 |
Thanked: 7,643 times |
Joined on Aug 2010
@ Wigan, UK
|
#3
|
Calculator.calc()
self.calc()
| The Following 2 Users Say Thank You to marxian For This Useful Post: | ||
|
|
12-04-2012
, 02:23 AM
|
|
|
Posts: 1,223 |
Thanked: 834 times |
Joined on Oct 2009
@ Michigan, USA / Jordan
|
#4
|

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.")
|
|
12-15-2012
, 10:39 PM
|
|
|
Posts: 1,223 |
Thanked: 834 times |
Joined on Oct 2009
@ Michigan, USA / Jordan
|
#5
|
| The Following User Says Thank You to bandora For This Useful Post: | ||
|
|
12-16-2012
, 05:38 AM
|
|
Posts: 2,587 |
Thanked: 2,173 times |
Joined on Oct 2009
@ Moon! It's not the East or the West side... it's the Dark Side
|
#7
|
| The Following User Says Thank You to Dave999 For This Useful Post: | ||
|
|
12-16-2012
, 02:11 PM
|
|
|
Posts: 1,223 |
Thanked: 834 times |
Joined on Oct 2009
@ Michigan, USA / Jordan
|
#8
|
![]() |
| Thread Tools | Search this Thread |
|
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:
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.")FarahFa.com • Qayyem.com
Last edited by bandora; 12-03-2012 at 10:01 PM.