forked from neilbalch/SimplePythonTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced Calculator.py
More file actions
171 lines (164 loc) · 6.17 KB
/
Advanced Calculator.py
File metadata and controls
171 lines (164 loc) · 6.17 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Import 'math' and 'random' Libraries
import math
import random
# Define Variables
output = 0
num1 = ""
operator = ""
num2 = ""
memStore = "Empty"
# Define Function Listing Function
def abilitiesList():
print("+...Addition")
print("-...Subtraction")
print("*...Multiplication")
print("/...Division")
print("^...Powers")
print("/-...Square Roots ")
print("!...Factorials (Input Cannot Be Negetave)")
print("Abs...Absolute Value")
print("d/r...Degrees To Radians")
print("r/d...Radians To Degrees")
print("pi...Returns PI")
print("e...Returs 'e'")
print("tau...Returns TAU (2xPI)")
print("M+...Save input to memory")
print("MR...Recall Memory")
print("M-...Clear Memory")
print("sin...Sine")
print("cos...Cosine")
print("tan...Tangent")
print("asin...Arc Sine")
print("acos...Arc Cosine")
print("atan...Arc Tangent")
print("log10...Log(10) of Input")
print("log...Returns The Apropriate Log of the Input (input1 is the log power)")
print("rand...Returns A Random Number Between 0 and 1")
print("randint...Returns A Random Number Between The Two Inputs")
print("//////////////////////////////////////////////////////////////////////////")
def askForNumInput(textPrompt):
# Devine local variable
convertedNum = math.nan
# Wait for valid numerical input
while True:
num = input(textPrompt)
try:
# Try to typecast the input to a float
float(num)
except ValueError:
# Catch the exception if it is not a number
print("ERROR: Syn: Invalid Num")
else:
# Typecasting
convertedNum = float(num)
break
return convertedNum
# While Loop
while True:
print("//////////////////////////////////////////////////////////////////////////")
print("Type 'help' for a list of abilities")
# Loop for getting operation
while True:
operator = input("What operation do you want to perform? ")
# Is operator == to any of out constants or predefines?
if operator == "help":
abilitiesList()
elif operator == "pi":
print(math.pi)
elif operator == "e":
print(math.e)
elif operator == "tau":
print(math.pi*2)
elif operator == "MR":
print(str(memStore))
elif operator == "M-":
memStore = "Empty"
print("Memory Cleared")
elif operator == "rand":
print(random.random())
# Has the user entered in a valid operator?
elif operator == "+" or operator == "-" or operator == "*" or operator == "/" or operator == "^" or operator == "/-" or operator == "!" or operator == "Abs" or operator == "d/r" or operator == "r/d" or operator == "M+" or operator == "M-" or operator == "MR" or operator == "sin" or operator == "cos" or operator == "tan" or operator == "asin" or operator == "acos" or operator == "atan" or operator == "log10" or operator == "log" or operator == "randint":
break
else:
print("ERROR: Invalid Operator")
# Loop for 1st number input
while True:
num1 = askForNumInput("First Number? ")
# Catch asin and acos out of bounds error case
if (operator == "asin" or operator == "acos") and (num1 > 1 or num1 < -1):
print("ERROR: Math: 'asin' and 'acos' commands only accept inputs in range -1 to +1")
elif operator == "!" and num1 < 0:
print("ERROR: Math: Factorials only accept inputs > 0")
else:
break
# Does the operation require a 2nd input?
if not (operator=="/-" or operator=="!" or operator=="Abs" or operator=="d/r" or operator=="r/d" or operator=="M+" or operator=="sin" or operator=="cos" or operator=="tan" or operator=="asin" or operator=="acos" or operator=="atan" or operator=="log10"):
# Loop for 2nd number input
while True:
num2 = askForNumInput("Second Number? ")
# Catch x/0 error case
if operator == "/" and num2 == "0":
print("ERROR: Math: Canot divide by 0!")
else:
break
# Calculations
if operator == "+":
output = num1 + num2
print("Your Answer: "+str(output))
elif operator == "-":
output = num1 - num2
print("Your Answer: "+str(output))
elif operator == "*":
output = num1 * num2
print("Your Answer: "+str(output))
elif operator == "/":
output = num1 / num2
print("Your Answer: "+str(output))
elif operator == "^":
output = math.pow(num1,num2)
print("Your Answer: "+str(output))
elif operator == "/-":
output = math.sqrt(num1)
print("Your Answer: "+str(output))
elif operator == "!":
output = math.factorial(num1)
print("Your Answer: "+str(output))
elif operator == "Abs":
output = math.fabs(num1)
print("Your Answer: "+str(output))
elif operator == "d/r":
output = math.radians(num1)
print("Your Answer: "+str(output))
elif operator == "r/d":
output = math.degrees(num1)
print("Your Answer: "+str(output))
elif operator == "M+":
memStore = num1
print("Number Stored")
elif operator == "sin":
output = math.sin(num1)
print("Your Answer: "+str(output))
elif operator == "cos":
output = math.cos(num1)
print("Your Answer: "+str(output))
elif operator == "tan":
output = math.tan(num1)
print("Your Answer: "+str(output))
elif operator == "asin":
output = math.asin(num1)
print("Your Answer: "+str(output))
elif operator == "acos":
output = math.acos(num1)
print("Your Answer: "+str(output))
elif operator == "atan":
output = math.atan(num1)
print("Your Answer: "+str(output))
elif operator == "log10":
output = math.log10(num1)
print("Your Answer: "+str(output))
elif operator == "log":
output = math.log(num2, num1)
print("Your Answer: "+str(output))
elif operator == "randint":
output = random.randint(num1, num2)
print("Your Answer: "+str(output))