# File: calc.py # Simple calculator # # Matt Bishop, COSMOS 2012, Cluster 4 # ############ # Supporting functions # # Does str contain a floating point number? # # PARAMETER: str: contains a string # RETURNS: True -- the string in str represents a floating point number # FLASE -- it does not # HOW IT WORKS: it tries to convert the string to a float # If there's a ValueError exception, it's not a float # If there isn't, it is # def isstrfloat(str): try: # do the conversion if you can value = float(str) except ValueError: return False else: return True # # Return a float that the user inputs # # PARAMETER: n: integer printed as part of the user prompt # RETURNS: floating point number that the user entered # HOW IT WORKS: read a string; if it's not a float, try again # def getnum(n): # prompt for, and read, a string try: temp = input("Number %d: " % n) except EOFError: return "q" # # see if it's a floating point number while not isstrfloat(temp): # nope -- get another string from the user print("You need to enter a number!") try: temp = input("Number %d: " % n) except EOFError: return "q" # # it corresponds to a float -- return the corresponding float return float(temp) # # Return a Python arithmetic operator (or 'q' for quit) that the # user inputs # # PARAMETER: none # RETURNS: a string containing a string representation of a Python # arithmetic operator # HOW IT WORKS: read a string; if it's not an operator, try again # def getop(): # prompt for, and read, a string try: what = input("What do you want to do (+ - * / % // ** q)? ") except EOFError: return "q" # # see if it's a Python operator or 'q' for quit while what != '+' and what != '-' and what != '*' and what != '/' and \ what != '//' and what != '%' and what != '**' and what != 'q': # nope -- get another string from the user print(what, "is not a valid operation -- try again") try: what = input("What do you want to do (+ - * / % // ** q)? ") except EOFError: return "q" # # it corresponds to an operator or 'q' -- return it return what # # The main function -- do the calculations until told to quit # def main(): # # announce the program print("Welcome to the calculator! ") # # loop until user says to quit cmd = 'start' while cmd != "q": # get the user's desired operation cmd = getop(); # if it's to quit, skip to the end of the loop if cmd != 'q': # # we are to do a computation # get the two operands operand1 = getnum(1) if operand1 == "q": return operand2 = getnum(2) if operand2 == "q": return # now do the operation and show the result if cmd == '+': # add print(operand1, "+", operand2, "=", operand1 + operand2) elif cmd == '-': # subtract print(operand1, "-", operand2, "=", operand1 - operand2) elif cmd == '*': # multiply print(operand1, "*", operand2, "=", operand1 * operand2) elif cmd == '/': # divide, remainder as a fraction print(operand1, "/", operand2, "=", operand1 / operand2) elif cmd == '%': # take the remainder print(operand1, "%", operand2, "=", operand1 % operand2) elif cmd == '//': # divide, discard the remainder print(operand1, "//", operand2, "=", operand1 // operand2) elif cmd == '**': # raise to a power print(operand1, "**", operand2, "=", operand1 ** operand2) else: # SHOULD NEVER HAPPEN -- anything other than the operators # and 'q' should be discarded by the getop() routine -- but # if that's changed and this function is not updated . . . print("Unknown operation", cmd) main()