0
defining spam?
def spam(a): def eggs(): a[:] = [5] def bacon(): a[0] += 1 eggs() bacon() return a[0] print(spam([2])) Output: 6 So lost on this don't even know where to begin? Don't know if they want me to find the fifth value of bacon which is a string? Or how they got up to 6 when the dictionary (maybe) said to go to 5? Any help appreciated.
2 odpowiedzi
+ 3
Spam defines two functions in its body: eggs and bacon.
One exchanges the contents of a to a single element 5, bacon adds 1 to that value.
Both read a from the outer function which is possible in Python.
So both these functions are called once. [2] to [5] to [6].
And then a[0] - the only element, being 6 - is returned.
(There are a few typos in the code btw. 5 should be in brackets and the definition has to end with :)
0
Thanks HF! One quick other question, how should I interpret " a[:] = [5]"? Dictionary a has a key of zero and undefined stop equals a list number 5? How did you know that spam defined 2 functions? More questions later (and thanks so much for your help). 👍🏻