How do I improve my code for Think Python, Exercise 7.4 eval and loop
Question: The task:

> Write a function called `eval_loop` that iteratively prompts the user, takes
> the resulting input and evaluates it using `eval()`, and prints the result.
>
> It should continue until the user enters `'done'`, and then return the value
> of the last expression it evaluated.

My code:

    
    
    import math
    
    def eval_loop(m,n,i):
        n = raw_input('I am the calculator and please type: ')
        m = raw_input('enter done if you would like to quit! ')
        i = 0   
        while (m!='done' and i>=0):
            print eval(n)
            eval_loop(m,n,i)
            i += 1
            break;
    
    eval_loop('','1+2',0)
    

My code cannot return the value of the last expression it evaluated!


Answer: Three comments:

  1. Using recursion for this means that you will eventually hit the system recursion limit, iteration is probably a better approach (and the one you were asked to take!); 
  2. If you want to `return` the result of `eval`, you will need to assign it; and
  3. I have no idea what `i` is for in your code, but it doesn't seem to be helping anything. 

With those in mind, a brief outline:

    
    
    def eval_loop():
        result = None
        while True:
            ui = raw_input("Enter a command (or 'done' to quit): ")
            if ui.lower() == "done":
                break
            result = eval(ui)
            print result
        return result
    

For a more robust function, consider wrapping `eval` in a `try` and dealing
with any errors stemming from it sensibly.

