-
Notifications
You must be signed in to change notification settings - Fork 0
/
bite159.py
40 lines (32 loc) · 936 Bytes
/
bite159.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
def simple_calculator(calculation):
"""Receives 'calculation' and returns the calculated result,
Examples - input -> output:
'2 * 3' -> 6
'2 + 6' -> 8
Support +, -, * and /, use "true" division (so 2/3 is .66
rather than 0)
Make sure you convert both numbers to ints.
If bad data is passed in, raise a ValueError.
"""
operation_list = ('+', '-', '*', '/')
calc_list = calculation.split(' ')
try:
op_1 = int(calc_list[0])
op_2 = int(calc_list[2])
sign = calc_list[1]
except:
raise ValueError
if sign not in operation_list:
raise ValueError
if sign == '+':
return op_1 + op_2
if sign == '-':
return op_1 -op_2
if sign == '*':
return op_1 * op_2
else:
try:
return op_1 / op_2
except:
raise ValueError
print(simple_calculator('1 / 0'))