Exercise 1

In this problem, you will write a closure to make writing messages easier. Suppose you write the following message all the time:

print("The correct answer is: {0:17.16f}".format(answer))

Wouldn't it be nicer to just write

correct_answer_is(answer)

and have the message? Your task is to write a closure to accomplish that. Here's how everything should work:

correct_answer_is = my_message(message)
correct_answer_is(answer)

The output should be: The correct answer is: 42.0000000000000000.

Now change the message to something else. Notice that you don't need to re-write everything. You simply need to get a new message function from my_message and invoke your new function when you want to.

You should feel free to modify this in any way you like. Creativity is encouraged!


In [4]:
def message(*answer):
    print ("correct_answer_is:{0:17.16f}".format(*answer))
    return()
def my_message(f):
    def find_answer(*answer):
        message(*answer)
    return find_answer
answer=41
correct_answer_is = my_message(message)
correct_answer_is(answer)


correct_answer_is:41.0000000000000000

In [ ]: