def check_operand(operand):
try:
return float(operand)
except ValueError:
print("wrong input value")
return None
def check_operator(operator):
if operator in ["+",'-',"*",'/']:
return True
else:
print("worng operator !")
return None
def check_odd(length):
if length % 2 and length >=3:
return True
else:
return False
expression = input("Please input expression with space for calculation :")
expression = expression.split()
# check a valid expression bdfore jump into while loop
if check_odd(len(expression)):
#valid length of expression
while(check_odd(len(expression))):
# argement = parameter
# length = len(expression)
x = expression[0]
op = expression[1]
y = expression[2]
# check first operand or exit the while loop witho
if check_operand(x):
x = check_operand(x)
else:
break
# check second operand or exit the while loop
if check_operand(y):
y = check_operand(y)
else:
break
#final check of opertor then perform calculation
if check_operand(op):
if op == '+':
result = x + y
if op == '-':
result = x - y
if op == '*':
result = x * y
if op == '/':
if y == 0:
print("value overflow !")
break
else:
result = x / y
else:
print("invalid operator")
break
del expresssion[:2]
expression[0] = result
else: # if expression is short then print the final result
print(f"The expression result is : {result}")
else:
print("invalid expression, can't calculate !")