Quick actions

cmd+k|ctrl+k

Navigation

Languages

calc2

Snippet info

Language

Python

Visibility

public

Author

middleklee

Created

2025-12-19T13:14:59.685286Z

Updated

2025-12-20T02:48:21.519087Z

def check_value(opand):
    try:
        if type(float(opand)) == float:
            return float(opand)
    except:
        print('wrong input data')
        return None
exp = input("Please input expression with spaces in between operand and operator")
exp = exp.split()
success = False
while len(exp) >= 3 and len(exp) % 2:
    x = exp[0]; y = exp[2]; op =exp[1]
    x = check_value(x); y = check_value(y)
    if x != None and y !=None:
        if op in ['+', '-', '*', '/']:
            if op == '+':
                result = x + y
            if op == '-':
                result = x - y
            if op == '*':
                result = x * y
            if op == '/':
                try:
                    result = x / y
                except:
                    print("second value can not be zero")
                    result = None
                    break
            del exp[:2]
            exp[0] = result
            success = True
        else:
            print("wrong operator")
            break
    else:
        break
else:
    if success:
        print(f"The calculated result is: {exp[0]}")
    else:
        print("wrong expression")
INFO