-
Notifications
You must be signed in to change notification settings - Fork 0
/
honest_calc.py
177 lines (137 loc) · 4.22 KB
/
honest_calc.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
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
172
173
174
175
176
177
ALLOWED_OPERATORS = '+-*/'
M = 'M'
Y, N = 'y', 'n'
def is_allowed(oper: str) -> bool:
"""Return True if s is one of the allowed operators."""
if len(oper) != 1:
raise ValueError('Bad operator. It has to be ONE of the '
f'following math operators: {ALLOWED_OPERATORS}')
if oper in ALLOWED_OPERATORS:
return True
else:
return False
def is_number(s: str) -> bool:
"""Return True if s can be cast to float.
If not, return False.
"""
try:
float(s)
except ValueError:
return False
else:
return True
def calculate(x: float, y: float, oper: str) -> float:
"""Return result of equation 'x oper y'.
If oper is not a valid operator, raise ValueError
"""
match oper:
case '+':
return x + y
case '-':
return x - y
case '*':
return x * y
case '/':
return x / y
case _:
raise ValueError(f"Wrong operator! '{oper}' "
f"not in '{ALLOWED_OPERATORS}'")
def keep_going() -> bool:
"""Return True if user wants to keep using the calculator.
If not, return False.
"""
while True:
answer = input('Do you want to continue calculations? (y / n):\n')
if answer == Y:
return True
elif answer == N:
return False
else:
continue
def remember() -> bool:
"""Return True if user wants to store the result.
If not, return False.
"""
while True:
answer = input('Do you want to store the result? (y / n):\n')
if answer == Y:
return True
elif answer == N:
return False
else:
continue
def is_one_digit(x) -> bool:
"""Return True if x is a one digit int, or False if it isn't.
"""
if -10 < x < 10 and x.is_integer():
return True
else:
return False
def check(x, y, oper) -> None:
"""Check if the user is a lazy little sloth.
Shame the user if they are.
"""
msg = ''
if is_one_digit(x) and is_one_digit(y):
msg += ' ... lazy'
if x == 1 or y == 1:
msg += ' ... very lazy'
if (x == 0 or y == 0) and oper in '*+-':
msg += ' ... very, very lazy'
if msg:
print(f'You are{msg}')
def really_remember(result: float) -> bool:
"""Return True if the result is clearly worth remembering.
If it isn't, attempt to coerce the user into discarding the result.
If after three (3) coercion attempts the user proves persistent,
return True.
If coercion proves successful, return False.
"""
coercions = (
'Are you sure? It is only one digit! (y / n)\n',
"Don't be silly! It's just one number! Add to the memory? (y / n)\n",
'Last chance! Do you really want to embarrass yourself? (y / n)\n'
)
# I'm not exactly sure if I like this implementation,
# but it is compact.
if is_one_digit(result):
for coercion in coercions:
if input(coercion) == N:
return False
return True
def calculator() -> None:
"""Run a sassy, if extremely rudimentary calculator."""
memory: float = 0.0 # Apparently it has to be a float.
while True:
equation = input('Enter an equation\n')
x, oper, y = equation.split()
if x == M:
x = memory
if y == M:
y = memory
if is_number(x) and is_number(y):
x, y = float(x), float(y)
else:
print('Do you even know what numbers are? Stay focused!')
continue
if not is_allowed(oper):
print("Yes ... an interesting math operation. "
"You've slept through all classes, haven't you?")
continue
check(x, y, oper)
try:
result = calculate(x, y, oper)
except ZeroDivisionError:
print('Yeah... division by zero. Smart move...')
continue
print(result)
if remember() and really_remember(result):
memory = result
if keep_going():
continue
else:
return
def main():
calculator()
if __name__ == "__main__":
main()