calc.py

Run Settings
LanguagePython
Language Version
Run Command
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))
Editor Settings
Theme
Key bindings
Full width
Lines