Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,69 @@
"""
Written by: Shreyas Daniel - github.com/shreydan
Description: Uses Pythons eval() function
as a way to implement calculator

Written by : Shreyas Daniel - github.com/shreydan
Description : Uses Pythons eval() function
as a way to implement calculator.
Functions available:

+ : addition
- : subtraction
* : multiplication
/ : division
% : percentage
sine: sin(rad)
cosine: cos(rad)
tangent: tan(rad)
square root: sqrt(n)
pi: 3.141......
--------------------------------------------
+ : addition
- : subtraction
* : multiplication
/ : division
% : percentage
e : 2.718281...
pi : 3.141592...
sine : sin(rad)
cosine : cos(rad)
tangent : tan(rad)
remainder : XmodY
square root : sqrt(n)
round to nearest integer : round(n)
convert degrees to radians : rad(deg)
"""

import math
import sys


def main():

def calc(k):

functions = ['sin', 'cos', 'tan', 'sqrt', 'pi','mod']
def calc(k):

k = k.replace(' ', '')
k = k.replace('^', '**')
k = k.replace('=', '')
k = k.replace('?', '')
k = k.replace('%', '/100')
k = k.replace('rad', 'radians')
k = k.replace('mod', '%')

for i in functions:
if i in k.lower():
withmath = 'math.' + i
k = k.replace(i, withmath)
functions = ['sin', 'cos', 'tan', 'sqrt', 'pi', 'radians', 'e']

try:
k = eval(k)
except ZeroDivisionError:
for i in functions:
if i in k.lower():
withmath = 'math.' + i
k = k.replace(i, withmath)

print("Can't divide by 0")
exit()
except NameError:
print('Invalid input')
exit()
try:
k = eval(k)
except ZeroDivisionError:
print("Can't divide by 0")
exit()
except NameError:
print('Invalid input')
exit()
except AttributeError:
print('Check usage method')
exit()

return k

return k

def result(k):
k = k.replace(' ', '')
k = k.replace('^', '**')
k = k.replace('=', '')
k = k.replace('?', '')
k = k.replace('%', '/100')
k = k.replace('mod', '%')
def result(k):
print("\n" + str(calc(k)))

print("\n" + str(calc(k)))

print("\nScientific Calculator\nEg: pi * sin(90) - sqrt(81)\nEnter quit to exit")
def main():

print("\nScientific Calculator\nEg: sin(rad(90)) + 50% * (sqrt(16)) + round(1.42^2) - 12mod3\nEnter quit to exit")

if sys.version_info.major >= 3:
while True:
Expand Down