Smart calculator
#!/usr/bin/python import cgi FORM = cgi.FieldStorage() print("Content-Type: text/html\n") ERROR = False try: XVAL = FORM['operand1'].value except KeyError: print("please enter first operand") ERROR = True try: YVAL = FORM['operand2'].value except KeyError: print("please enter second operand") ERROR = True if not ERROR: try: CONV = eval(FORM['convert'].value) except: CONV = False OPER = FORM['operator'].value if not CONV: EXPR = XVAL + ' ' + OPER + ' ' + YVAL else: EXPR = str(float(XVAL)) + ' ' + OPER \ + ' ' + str(float(YVAL)) RESULT = EXPR + ' = ' + str(eval(EXPR)) print("") print("
Comments
Post a Comment