Quick actions

cmd+k|ctrl+k

Navigation

Languages

14-May-2026 Lesson 9

Snippet info

Language

Python

Visibility

public

Author

michaelluiwaihung

Created

2026-05-14T13:07:10.891928Z

Updated

2026-05-14T13:56:29.396638Z

#temp 16




def check_value(num):
    try:
        num = float(num)
        return True, num
    except ValueError:
        print("invalid data")
        return False, 

def check_op(operator):
    if operator in ['+', '-', '*', '/']:
        return True
    else:
        return False

def calc(v1, v2, oper):
    if v1[0] and v2[0] and oper:
        if op == "+":
            result = v1[1] + v2[1]
        if op == "-":
            result = v1[1] - v2[1]
        if op == "*":
            result = v1[1] * v2[1]
        if op == "/":
            if v2[1] == 0:
                result = False
            else:
                result = v1[1] / v2[1]
        print(result)
        return result
    else:
        print("wrong expression")
        return False

exp = input("please input expression with spaces : ")
ls = exp.split()

while len(ls) >= 3 and len(ls) % 2:
    x = ls[0]
    y = ls[2]
    op = ls[1]
    result = calc(check_value(x), check_value(y), check_op(op))
    if result != False:
       del ls[:2]
       ls[0] = result
    else:
       break
    
#print(ls)
    
INFO