-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_calculator_01_hard.py
62 lines (45 loc) · 1.41 KB
/
simple_calculator_01_hard.py
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
"""
Shows one way to implement a solution to Challenge Exercise 4 (simple calculator)
@author: sql.sith
"""
from sys import version_info
if version_info.major < 3:
input = raw_input
def calc_this(int1, op, int2):
if op == "+":
return int1 + int2
elif op == "-":
return int1 - int2
elif op == "*":
return int1 * int2
elif op == "/":
return float(int1) / float(int2)
else:
_INVALID_OPERATOR = "Invalid operator: " + op
print("The only operators supported are: +-/*")
raise(Exception(_INVALID_OPERATOR))
_debug = False
print("Goal 1\n")
typedInput = input("Enter an arithmetic problem in the form INT1 OPERATOR INT2: ")
# typedInputSaved = typedInput
# get the first number, one hard way (there are several):
firstNumberString = ""
for ch in typedInput:
if ch != " ":
firstNumberString += ch
else:
# this exits the loop:
break
firstNumber = int(firstNumberString)
# trim the first number and the first space:
typedInput = typedInput[len(firstNumberString) + 1:]
operator = typedInput[:1]
# trim operator and second space:
typedInput = typedInput[2:]
# what's left is the second number:
secondNumber = int(typedInput)
if _debug:
print("firstNumber: " + str(firstNumber))
print("operator: " + operator)
print("secondNumber: " + str(secondNumber))
print (calc_this(firstNumber, operator, secondNumber))