Question 1 (Exercise 7.2 from Think Python 2nd Edition by Allen Downey) Built-in
ID: 2312330 • Letter: Q
Question
Question 1 (Exercise 7.2 from Think Python 2nd Edition by Allen Downey) Built-in function eval takes a string and evaluates it using the Python interpreter. For example: >>eval('1 + 2* 3') >>>import math eval'math.sqrt(5)") 2.23606797749979 eval'type (math.pi)") Write a fucntion called eval_loop that iteractively 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 returns the value of the last expression it evaluated.Explanation / Answer
# The built-in function eval takes a string and evaluates it using the Python interpreter.
def eval_loop():
while True:
n = raw_input('Input? :: ')
if n == 'done':
break
else:
result = eval(n)
print result
print result
eval_loop()
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.