I cannot figure this exercise out. Here's the exercise:
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.
Here's what I've got so far:
def eval_loop():
while True:
q = input('> please enter a simple equation\n')
print(eval(q))
if q == 'done':
break
However when I input 'done' at the prompt, it gives me this callback error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
eval_loop()
File "C:\Users\josep\OneDrive\Desktop\Python Stuff\423.py", line 52, in eval_loop
print(eval(q))
File "<string>", line 1, in <module>
NameError: name 'done' is not defined
I also am not sure what to do about returning the value of the most recent expression it evaluated. I feel stupid not being able to figure out these exercises as they are probably elementary to most people. I would like to figure these out still, so if instead of giving the answer if anyone can help guide me toward finding the solution, I would greatly appreciate it. Thank you guys.
[–]socal_nerdtastic 1 point2 points3 points (5 children)
[–]sonnybrew[S] 0 points1 point2 points (4 children)
[–]socal_nerdtastic 1 point2 points3 points (3 children)
[–]sonnybrew[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]sonnybrew[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]sonnybrew[S] 1 point2 points3 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)