Quick actions

cmd+k|ctrl+k

Navigation

Languages

calc-10

Snippet info

Language

Python

Visibility

public

Author

aaron.lung.wai

Created

2025-12-10T09:53:11.298321Z

Updated

2025-12-10T09:53:17.884758Z

exp = input("PLease input expression with space between operands and operator : ")
exp = exp.split()
def check_op(x):
    return True if x in ['+','-','*','/'] else print("wrong operator") or False
def check_value(x):
    try: 
        return float(x)
    except ValueError:
        return  print("wrong input") or False
result = 0
flag = False
while len(exp) >= 3 and len(exp) %2:
    x = exp[0]
    op = exp[1]
    y = exp[2]
    x = check_value(x) 
    y = check_value(y)
    if check_op(op) and x is not False and y is not False:
        if op == '+':
            result = x+y
        elif op == '-':
            result = x-y
        elif op == '*':
            result = x*y
        elif op == '/':
            if y == 0:
                print("cannot divide by zero")
                break
            else:
                result =x/y
        del exp[:2]
        exp[0] = result 
        flag = True       
else :
    if flag:
        print(f"result : {exp[0]}") 
    else :    
        print("wrong expression input")          
          
INFO