|
3 | 3 | import random |
4 | 4 | # Define Variables |
5 | 5 | output = 0 |
6 | | -process1 = "" |
7 | | -processO = "" |
8 | | -process2 = "" |
| 6 | +num1 = "" |
| 7 | +operator = "" |
| 8 | +num2 = "" |
9 | 9 | memStore = "EMPTY" |
10 | | -# Define Vars To Allow Program To Pass The Input Stage |
11 | | -operationD = False |
12 | | -process1D = False |
13 | | -process2D = False |
14 | | -need2 = True |
15 | | -skipOp = False |
16 | 10 |
|
17 | 11 | # Define Function Listing Function |
18 | 12 | def abilitiesList(): |
@@ -44,162 +38,135 @@ def abilitiesList(): |
44 | 38 | print("randint...Returns A Random Number Between The Two Inputs") |
45 | 39 | print("//////////////////////////////////////////////////////////////////////////") |
46 | 40 |
|
| 41 | +def askForInput(textPrompt): |
| 42 | + num = input(textPrompt) |
| 43 | + try: |
| 44 | + # Try to typecast the input to a float |
| 45 | + float(num) |
| 46 | + except ValueError: |
| 47 | + # Catch the exception if it is not a number |
| 48 | + print("ERROR: Syn: Invalid Num") |
| 49 | + else: |
| 50 | + # Else, move on |
| 51 | + # Typecasting |
| 52 | + return float(num) |
| 53 | + |
47 | 54 | # While Loop |
48 | | -while(1): |
| 55 | +while(True): |
49 | 56 | # Reset Variables |
50 | | - operationD = False |
51 | | - process1D = False |
52 | | - process2D = False |
53 | 57 | print("//////////////////////////////////////////////////////////////////////////") |
54 | 58 | print("Type 'help' for a list of abilities") |
55 | | - # Operation Input |
56 | | - while(operationD == False): |
57 | | - #print("Hello, What is your Operation (Operator)?") |
58 | | - processO = input("Hello, What is your Operation (Operator)? ") |
59 | | - # Is processO == to any of out constants or predefines? |
60 | | - if processO == "help": |
| 59 | + # Loop for getting operation |
| 60 | + while True: |
| 61 | + operator = input("What operation do you want to perform? ") |
| 62 | + # Is operator == to any of out constants or predefines? |
| 63 | + if operator == "help": |
61 | 64 | abilitiesList() |
62 | | - processO = "" |
63 | | - skipOp = True |
64 | | - elif processO == "pi": |
| 65 | + operator = "" |
| 66 | + elif operator == "pi": |
65 | 67 | print(math.pi) |
66 | | - processO = "" |
67 | | - skipOp = True |
68 | | - elif processO == "e": |
| 68 | + operator = "" |
| 69 | + elif operator == "e": |
69 | 70 | print(math.e) |
70 | | - processO = "" |
71 | | - skipOp = True |
72 | | - elif processO == "tau": |
| 71 | + operator = "" |
| 72 | + elif operator == "tau": |
73 | 73 | print(math.pi*2) |
74 | | - processO = "" |
75 | | - skipOp = True |
76 | | - elif processO == "MR": |
| 74 | + operator = "" |
| 75 | + elif operator == "MR": |
77 | 76 | print(str(memStore)) |
78 | | - processO = "" |
79 | | - skipOp = True |
80 | | - elif processO == "M-": |
| 77 | + operator = "" |
| 78 | + elif operator == "M-": |
81 | 79 | memStore = "Empty" |
82 | 80 | print("Memory Cleared") |
83 | | - processO = "" |
84 | | - skipOp = True |
85 | | - elif processO == "M+": |
86 | | - process2 = "0" |
87 | | - operationD = True |
88 | | - skipOp = True |
89 | | - if processO=="rand": |
| 81 | + operator = "" |
| 82 | + elif operator == "M+": |
| 83 | + num2 = "0" |
| 84 | + elif operator == "rand": |
90 | 85 | print(random.random()) |
91 | | - skipOp = True |
92 | | - # Have We Given process1 a Valid Operator? |
93 | | - elif processO == "+" or processO== "-" or processO== "*" or processO== "/" or processO== "^" or processO=="/-" or processO=="!" or processO=="Abs" or processO=="d/r" or processO=="r/d" or processO=="M+" or processO=="M-" or processO=="MR" or processO=="sin" or processO=="cos" or processO=="tan" or processO=="asin" or processO=="acos" or processO=="atan" or processO=="log10" or processO=="log" or processO=="randint": |
94 | | - operationD = True |
95 | | - elif skipOp == True: |
96 | | - processO = "" |
| 86 | + # Has the user entered in a valid operator? |
| 87 | + 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": |
| 88 | + break |
97 | 89 | else: |
98 | 90 | print("ERROR: Invalid Operator") |
99 | | - # Do We Need A 2nd Input? |
100 | | - if processO=="/-" or processO=="!" or processO=="Abs" or processO=="d/r" or processO=="r/d" or processO=="M+" or processO=="sin" or processO=="cos" or processO=="tan" or processO=="asin" or processO=="acos" or processO=="atan" or processO=="log10": |
101 | | - need2 = False |
102 | | - else: |
103 | | - need2 = True |
104 | | - # 1st Number Input |
105 | | - while(process1D == False): |
106 | | - #print("First Number?") |
107 | | - process1 = input("First Number? ") |
108 | | - try: |
109 | | - float(process1) |
110 | | - except ValueError: |
111 | | - print("ERROR: Syn: Invalid Num") |
112 | | - else: |
113 | | - if processO == "asin" or processO == "acos": |
114 | | - if int(process1) > 1 or int(process1) < -1: |
115 | | - print("ERROR: Math: 'asin' and 'acos' commands only accept inputs in range -1 to +1") |
116 | | - process1D = False |
117 | | - else: |
118 | | - process1D = True |
119 | | - else: |
120 | | - process1D = True |
121 | | - # 2nd Number Input |
122 | | - while(process2D == False): |
123 | | - if need2 == False: |
124 | | - process2 = "0" |
125 | | - process2D = True |
| 91 | + |
| 92 | + # Loop for 1st number input |
| 93 | + while True: |
| 94 | + num1 = askForInput("First Number? ") |
| 95 | + # Catch asin and acos out of bounds error case |
| 96 | + if (operator == "asin" or operator == "acos") and (float(num1) > 1 or float(num1) < -1): |
| 97 | + print("ERROR: Math: 'asin' and 'acos' commands only accept inputs in range -1 to +1") |
126 | 98 | else: |
127 | | - #print("Second Number?") |
128 | | - process2 = input("Second Number? ") |
129 | | - try: |
130 | | - float(process2) |
131 | | - except ValueError: |
132 | | - print("ERROR: Syn: Invalid Num") |
| 99 | + break |
| 100 | + |
| 101 | + # Does the operation require a 2nd input? |
| 102 | + 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"): |
| 103 | + # Loop for 2nd number input |
| 104 | + while True: |
| 105 | + num2 = askForInput("Second Number? ") |
| 106 | + # Catch x/0 error case |
| 107 | + if operator == "/" and num2 == "0": |
| 108 | + print("ERROR: Math: Canot divide by 0!") |
133 | 109 | else: |
134 | | - process2D = True |
135 | | - # x/0 Error |
136 | | - if processO == "/" and process2 == "0": |
137 | | - #Error |
138 | | - print("ERROR: Math: Canot divide by 0!") |
139 | | - else: |
140 | | - # Typecasting |
141 | | - intProcess1 = float(process1) |
142 | | - # Check if to Typecast intProcess2 |
143 | | - if need2 == True: |
144 | | - intProcess2 = float(process2) |
145 | | - # Strategic Processing |
146 | | - if processO == "+": |
147 | | - output=intProcess1+intProcess2 |
148 | | - print("Your Answer: "+str(output)) |
149 | | - if processO == "-": |
150 | | - output=intProcess1-intProcess2 |
151 | | - print("Your Answer: "+str(output)) |
152 | | - if processO == "*": |
153 | | - output=intProcess1*intProcess2 |
154 | | - print("Your Answer: "+str(output)) |
155 | | - if processO == "/": |
156 | | - output=intProcess1/intProcess2 |
157 | | - print("Your Answer: "+str(output)) |
158 | | - if processO == "^": |
159 | | - output=math.pow(intProcess1,intProcess2) |
160 | | - print("Your Answer: "+str(output)) |
161 | | - if processO == "/-": |
162 | | - output=math.sqrt(intProcess1) |
163 | | - print("Your Answer: "+str(output)) |
164 | | - if processO == "!": |
165 | | - output=math.factorial(intProcess1) |
166 | | - print("Your Answer: "+str(output)) |
167 | | - if processO == "Abs": |
168 | | - output=math.fabs(intProcess1) |
169 | | - print("Your Answer: "+str(output)) |
170 | | - if processO=="d/r": |
171 | | - output=math.radians(intProcess1) |
172 | | - print("Your Answer: "+str(output)) |
173 | | - if processO=="r/d": |
174 | | - output=math.degrees(intProcess1) |
175 | | - print("Your Answer: "+str(output)) |
176 | | - if processO=="M+": |
177 | | - memStore = process1 |
178 | | - print("Number Stored") |
179 | | - if processO=="sin": |
180 | | - output=math.sin(intProcess1) |
181 | | - print("Your Answer: "+str(output)) |
182 | | - if processO=="cos": |
183 | | - output=math.cos(intProcess1) |
184 | | - print("Your Answer: "+str(output)) |
185 | | - if processO=="tan": |
186 | | - output=math.tan(intProcess1) |
187 | | - print("Your Answer: "+str(output)) |
188 | | - if processO=="asin": |
189 | | - output=math.asin(intProcess1) |
190 | | - print("Your Answer: "+str(output)) |
191 | | - if processO=="acos": |
192 | | - output=math.acos(intProcess1) |
193 | | - print("Your Answer: "+str(output)) |
194 | | - if processO=="atan": |
195 | | - output=math.atan(intProcess1) |
196 | | - print("Your Answer: "+str(output)) |
197 | | - if processO=="log10": |
198 | | - output=math.log10(intProcess1) |
199 | | - print("Your Answer: "+str(output)) |
200 | | - if processO=="log": |
201 | | - output=math.log(intProcess2, intProcess1) |
202 | | - print("Your Answer: "+str(output)) |
203 | | - if processO=="randint": |
204 | | - output=random.randint(intProcess1, intProcess2) |
205 | | - print("Your Answer: "+str(output)) |
| 110 | + break |
| 111 | + |
| 112 | + # Calculations |
| 113 | + if operator == "+": |
| 114 | + output = num1 + num2 |
| 115 | + print("Your Answer: "+str(output)) |
| 116 | + if operator == "-": |
| 117 | + output = num1 - num2 |
| 118 | + print("Your Answer: "+str(output)) |
| 119 | + if operator == "*": |
| 120 | + output = num1 * num2 |
| 121 | + print("Your Answer: "+str(output)) |
| 122 | + if operator == "/": |
| 123 | + output = num1 / num2 |
| 124 | + print("Your Answer: "+str(output)) |
| 125 | + if operator == "^": |
| 126 | + output = math.pow(num1,num2) |
| 127 | + print("Your Answer: "+str(output)) |
| 128 | + if operator == "/-": |
| 129 | + output = math.sqrt(num1) |
| 130 | + print("Your Answer: "+str(output)) |
| 131 | + if operator == "!": |
| 132 | + output = math.factorial(num1) |
| 133 | + print("Your Answer: "+str(output)) |
| 134 | + if operator == "Abs": |
| 135 | + output = math.fabs(num1) |
| 136 | + print("Your Answer: "+str(output)) |
| 137 | + if operator == "d/r": |
| 138 | + output = math.radians(num1) |
| 139 | + print("Your Answer: "+str(output)) |
| 140 | + if operator == "r/d": |
| 141 | + output = math.degrees(num1) |
| 142 | + print("Your Answer: "+str(output)) |
| 143 | + if operator == "M+": |
| 144 | + memStore = num1 |
| 145 | + print("Number Stored") |
| 146 | + if operator == "sin": |
| 147 | + output = math.sin(num1) |
| 148 | + print("Your Answer: "+str(output)) |
| 149 | + if operator == "cos": |
| 150 | + output = math.cos(num1) |
| 151 | + print("Your Answer: "+str(output)) |
| 152 | + if operator == "tan": |
| 153 | + output = math.tan(num1) |
| 154 | + print("Your Answer: "+str(output)) |
| 155 | + if operator == "asin": |
| 156 | + output = math.asin(num1) |
| 157 | + print("Your Answer: "+str(output)) |
| 158 | + if operator == "acos": |
| 159 | + output = math.acos(num1) |
| 160 | + print("Your Answer: "+str(output)) |
| 161 | + if operator == "atan": |
| 162 | + output = math.atan(num1) |
| 163 | + print("Your Answer: "+str(output)) |
| 164 | + if operator == "log10": |
| 165 | + output = math.log10(num1) |
| 166 | + print("Your Answer: "+str(output)) |
| 167 | + if operator == "log": |
| 168 | + output = math.log(num2, num1) |
| 169 | + print("Your Answer: "+str(output)) |
| 170 | + if operator == "randint": |
| 171 | + output = random.randint(num1, num2) |
| 172 | + print("Your Answer: "+str(output)) |
0 commit comments