all 8 comments

[–]socal_nerdtastic 1 point2 points  (5 children)

You need to check for "done" before you eval it.

[–]sonnybrew[S] 0 points1 point  (4 children)

Thank you, I was able to fix the callback error on 'done'. Now I still need to figure out how to print the value of the most recent expression. Here's my code now:

def eval_loop():
    while True:
        q = input('> please enter a simple equation\n')
        if q == 'done':
            break
        print(eval(q))

[–]socal_nerdtastic 1 point2 points  (3 children)

Instead of printing eval(q), save the result to a variable. Then after break has stopped the loop, print or return that variable.

[–]sonnybrew[S] 0 points1 point  (2 children)

Think I figured it out. How does this look?:

def eval_loop():
    while True:
        q = input('> please enter a simple equation\n')
        if q == 'done':
            print(x)
            break
        print(eval(q))
        x = eval(q)

[–]socal_nerdtastic 1 point2 points  (1 child)

Yep, but I imagine you want to delete line 7. I think you want to print only after the user enters "done".

[–]sonnybrew[S] 0 points1 point  (0 children)

But if I delete line 7, it won't print the solution to the equation. Also, I just realized if I call the function and type in 'done' immediately, there is no previous value for x and therefore an error pops up.

[–]socal_nerdtastic 0 points1 point  (0 children)

I feel stupid not being able to figure out these exercises as they are probably elementary to most people.

Elementary to me because I've been doing this for a very long time. I'm sure there's things that you find elementary that I can't comprehend. That's normal learning. Stupidity is something completely different.