Quick actions

cmd+k|ctrl+k

Navigation

Languages

calc.py

Snippet info

Language

Python

Visibility

public

Author

wingoryin

Created

2025-07-02T11:22:31.456396Z

Updated

2025-07-23T12:52:35.525752Z

def check_num(num):
    try:
        return float(num)
    except ValueError:
        print("input error" + str(num))
        exit()
def check_op(operator):
    if operator in ["+","-","*","/"]:
        return True
    else:
        print("Wrong operator")
        return False

#try:
    #a = float(a)
    #b = float(b)
   # if op in ["+","-","*","/"]:
        #print("op is right")
def calc(a,b,op):
    a = check_num(a)
    b = check_num(b)
    print(a,b)
    if check_op(op):
        if op == '+':
            result = a + b
        if op == '-':
            result = a - b
        if op == '*':
            result = a * b
        if op == '/':
            if b == 0:
                print("** b can not be zero **")
            else :
                result = a/b
    return result
    result = 0
    #else:
        #print("wrong operator")
#except:
    #print("Error")
#except ZeroDivisionError :
    #print("b can't be zero")
#    print("data type error")
#exp = '1+1'
exp = '1 + 1'
exp = exp.split()
print(exp)
#a = -2
#b = 9
#op = '/'

while (len(exp) >=3 and len(exp)%2):
    a = exp[0]
    b = exp[2]
    op = exp[1]
    result = calc(a,b,op)
    del exp[:2]
    print(exp)
    exp[0] = result
print ("expression result is:" + str(result))
INFO