Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Why does this program stop with a NameError? Fix it.
def make_greeting(name):
message = "Hello, " + name
make_greeting("Asha")
print(message)Variables made inside a function are local. Use return to send the answer out.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Why. The variable message is made inside the function. It is local: it exists only while the function runs. Outside, Python does not know the name message. Also, the function gives nothing back.
Fix. Make the function return the message, and store it where it is called:
Log in to join the discussion.
Loading…