# Tutorial 3 - Problem 5 # # 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. # # Let's only consider equations of the form: operand> # # Anne Lavergne # Dec. 2015 def eval_loop( ): result = 0 done = False while not done: equation = input("Please, enter an equation (format: ): ") if len(equation) == 0: print("You have not entered anything! Try again!") else: if equation.isalpha( ): print("Have you entered '{}'? Try again!". format(equation)) else: equationList = equation.split( ) if 1 <= len(equationList) < 3: print("Have you entered a complete equation? Try again!") else : result = eval(equation) print(equation + " = %f" %result) userReply = input("If you wish to stop, please, enter 'done' otherwise press any key: ") if userReply.lower() == 'done' : done = True return result # Main part of program theResult = eval_loop( ) # Actual results: ##Please, enter an equation (format: ): ##You have not entered anything! Try again! ##Please, enter an equation (format: ): Banana ##Have you entered 'Banana'? Try again! ##Please, enter an equation (format: ): 2 4 ##Have you entered a complete equation? Try again! ##Please, enter an equation (format: ): 2 + 4 ##2 + 4 = 6.000000 ##If you wish to stop, please, enter 'done' otherwise press any key: done