Skip to content

Instantly share code, notes, and snippets.

@rudrathegreat
Last active November 25, 2018 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudrathegreat/9fa8578ae83664baef182948d4f73dac to your computer and use it in GitHub Desktop.
Save rudrathegreat/9fa8578ae83664baef182948d4f73dac to your computer and use it in GitHub Desktop.
Simple Calculator with Functionality to Use Previous Answer
'''
This class is designed to be used for mathematical
calculations only.
This program has the functionality to remember the
previous answer which you needed to calculate.
This code was orginally proposed in this issue,
link below -
https://github.com/AceLewis/my_first_calculator.py/issues/24
This program is deviated from Calculator.py. The link
for that is below -
https://gist.github.com/rudrathegreat/de0f463c7304af152cccd34ce0cd5a61
NumPy is a Python module programmed in Python, C
and FORTAN designed to do fast calculations in
Python. This is because Python is a very slow
program and when using the default operations
in Python, it often doen't give you the coreect
answer and it takes a long time to do so.
The math module is really good for many operations
found in a scientific calculator. It includes log,
sqrt, powers, etc.
More functionality coming soon!
'''
import math
import numpy as np
class Calculator(object):
def __init__(self):
self.answer = 0
def add(self, num1, num2):
try:
if str(num1) == 'Ans':
answer = np.int(self.answer) + num2
else:
answer = np.int(num1) + np.int(num2)
self.answer = answer
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def subtract(self, num1, num2):
try:
if str(num1) == 'Ans':
answer = np.int(self.answer) - num2
else:
answer = np.int(num1) - np.int(num2)
self.answer = answer
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def multiply(self, num1, num2):
try:
if str(num1) == 'Ans':
answer = np.int(self.answer) * num2
else:
answer = np.int(num1) + np.int(num2)
self.answer = answer
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def divide(self, num1, num2):
try:
if str(num1) == 'Ans':
answer = np.int(self.answer) / num2
else:
answer = np.int(num1) / np.int(num2)
self.answer = answer
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment