def check_num(op):
try:
num = float(op)
return True, num
#except ValueError as d:
#print("Cannot convert into value !")
#print (d)
#return False,
except Exception as e:
#print('Invalid Input')
print (e)
return False,
def check_op(operator):
if operator in ('+', '-', '*', '/'):
return True, operator
else:
return False,
def calc(x, y, z):
if x[0] and y[0] and z[0]:
if z[1] == '+':
result = x[1] + y[1]
return result
if z[1] == '-':
result = x[1] - y[1]
return result
if z[1] == '*':
result = x[1] * y[1]
return result
if z[1] == '/':
if y[1] == 0:
return False
else:
result = x[1] / y[1]
return result
else:
print("wrong expression")
return False
#print(calc(check_num(a), check_num(b), check_op(c)))
exp = '4 + 2 * 3'
ls = exp.split()
while len(ls) >= 3 and len(ls) % 2:
x = ls[0]
y = ls[2]
op = ls[1]
result = calc(check_num(x), check_num(y), check_op(op))
if result != False:
del ls[:2]
ls[0] = result
else:
break
print(ls)