Question 1 Exercise 72 from Think Python 2nd Edition by Alle

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.

Solution

# 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()

 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. Fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site