This is an example from my textbook, but I cant wrap my head around why it works, can someone break it down? I’m having trouble understanding functions and loops. ( I understand the %s formatting part)
def knight(saying):
def inner(quote):
return “We are the knights who say: ‘%s’” % quote
return inner(saying)
knight(“NI!”)
“We are the knights who say: ‘NI!’”
Enclose the code in triple ` (backtick) such that the formatting is preserved.
knight(“NI!”)
is a call to knight
with the argument saying
taking the value “NI!”
.
This function returns the value of the expression inner(saying)
, where inner
is an inner function. So, the value of that expression comes from calling the function inner
with the argument quote
taking the value of saying
, which is "NI!"
. In turn, inner
returns the formatted string that you say you understand, which results in
“We are the knights who say: ‘NI!’”
thank you that definetly makes it clearer, ill have to read it a few times for it to sink in. i appreciate you taking the time.