-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator.py
More file actions
98 lines (81 loc) · 2.7 KB
/
calculator.py
File metadata and controls
98 lines (81 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#calculator
import math
def addition(a, b):
'''addtion of two num'''
add = round(a + b, 2)
print("The sum of", a, 'and', b, 'is', add)
return add
def subtraction(a, b):
'''subtraction of two num'''
sub = round(a - b, 2)
print("The subtraction of", a, 'and', b, 'is', sub)
return sub
def multipication(a, b):
'''multiply of two numbers'''
mul = round(a * b, 2)
print("The multipication of", a, 'and', b, 'is', mul)
return mul
def division(a, b):
'''a divided by b'''
if b == 0:
return 'Can not divide by zero'
else:
div = round(a / b,2)
print("The division of", a, 'and', b, 'is', div)
return div
def exponatation(a, b):
'''a to the power b'''
ex = round(math.pow(a, b), 2)
print("The result of", a, 'raised to the', b, 'power is', ex)
return ex
def history(operations):
'''after doing all calculations it will print out all the calculations done'''
print("\nCalculation Summary:")
for x in operations:
if type(x) != str:
print(str(x[0]) + ' ' + str(x[2]) +' ' + str(x[1]) + ' = ' + str(x[3]))
else:
print(x)
def calculate_more():
'''ask user to do more calculations'''
y_n = input("Do want to perform more calculation(y/n): ")
if y_n == 'y':
return True
else:
return False
def user_input():
value = float(input("Enter a value: "))
return value
def perform_calculation(a, b):
'''it will ask user what calculation you want to perform'''
perform = input("\nEnter a operation('addition(+)', 'subtraction(-)', 'multiply(*)', 'divide(/)', 'exponential(^)'): ")
if perform == '+':
result = addition(a, b)
elif perform == '-':
result = subtraction(a, b)
elif perform == '*':
result = multipication(a, b)
elif perform == '/':
result = division(a, b)
if type(result) == str:
print(result)
return 'DIV ERROR'
elif perform == '^':
result = exponatation(a, b)
else:
print('Invalid Operation, TRY AGAIN')
return 'OOP ERROR'
return [a, b, perform, result]
print('Welcome to python calculator!!')
#print(calculate_more())
performing = True
operation = []
while performing:
print('Enter the two numbers and then choose the desired operation')
a = user_input()
b = user_input()
result = perform_calculation(a,b)
operation.append(result)
performing = calculate_more()
history(operation)
print('Thank you for using The Python Calculator App.\tGoodbye.')