1) Given a string of the form:

    "1 + 6"

write a Python function compute(thestring) that will compute the sum and return its value, e.g.

    mystring = "1 + 6"
    print compute(mystring)
    7

In [6]:
def compute(astring):
    expression_list = astring.split()
    #print expression_list
    num1 = float(expression_list[0])
    num2 = float(expression_list[2]) # or expression_list[-1]
    result = num1 + num2
    return result

In [7]:
def compute(astring):
    number_list = astring.split('+')
    print number_list
    total = 0
    for number in number_list:
        total += float(number)
    return total

In [11]:
def compute(astring):
    expression_list = astring.split()
    total = float(expression_list[0])
    operation = ''
    for element in expression_list[1:]:
        if element == '+':
            operation = element
        else:
            total += float(element)
    return total

In [15]:
# test compute
mystring = "103 + 7"
print compute(mystring) # should print 110
assert compute("1 + 1") == 2


110

2) Make your compute function handle the basic arithmetic operators: + (add), - (subtract), * (multiply) and / (divide). Division should be floating point division. So now e.g.

    mystring = "2 * 4"
    print compute(mystring)
    8

    mystring = "7 - 9"
    print compute(mystring)
    -2

3) Use compute in a script so that you can process a file containing these mathematical expressions and print an answer for each line. E.g. if myfile.txt contains:

    2 * 4
    7 - 9
    8 + 1
    9 / 2

If your script is called compute.py then running it should look like:

    compute.py myfile.txt
    8
    -2
    9
    4.5

4) Make your compute.py script write the results into a file, so now the compute.py script will take two command line arguments, the input file and the output file, and running it will look like:

    compute.py myfile.txt results.txt

where for the example given above the results (8, -2, 9, 4.5) should be written to results.txt.

5) Expand the compute function so that it can process more than 2 numbers. Operations should be processed left to right without concern for the normal rules of arithmetic (i.e. the BODMAS rule doesn't apply). So e.g.

mystring = "2 + 7 - 8 * 3"
print compute(mystring)
3

Tools in your toolbox:

Remember that:

  1. You can split a string into a list of strings with mystring.split()

  2. A string can be converted to a number with int() or float()

  3. The conditional statement if lets you choose what to do in your code, e.g.:

     if fruit == 'lemon':
         print "sour"
     elif fruit == 'strawberry':
         print "sweet"
     else:
         print "I don't know"
  4. The open() built in function, when given a filename, returns a file open for reading or writing e.g.

     myfilename = 'myfile.txt'
     myfile = open(myfilename)
    
     myfilename = 'output.txt'
     myfile = open(myfilename, 'w')
  5. You can loop through the lines in a file or the elements of a list with for:

     for line in myfile:
         print "the line is:", line
  6. The command line arguments to a script are in sys.argv:

     import sys
    
     print "I got:", sys.argv

In [ ]: