python question! please show code and steps! Lab Question 1 (Nested Squares) [5
ID: 3668355 • Letter: P
Question
python question!
please show code and steps!
Lab Question 1 (Nested Squares) [5 points] with bonus: A. The picture to the right consists of 10 squares. The outermost is 200 wide and each inner one is 20 smaller. They are on the left and top 5 apart. Write a Python function nestedsquares that draws this picture using turtle graphics. The function should start with reset and hideturtle and use one or more loops B. Write a function artisti that draws nested squares according to your imagination: you can draw the lines in different colors, you can color the squares, you can vary the distance between squares according to some scheme, you can tilt each square, you can have more squares, the picture can be larger. The only constraint is that is the picture has to consist only L of squares, they have to be nested in some way, and a loop is used to draw the squares. Post the generated picture as well as your Python code on Discussions Galleries on Avenue for everyone to see You can only post once, so be sure that you post the final version. Up-vote the ones you like most. The TAs and the instructor vote as well. The ones with the most up-votes get bonus marks Place both functions in the file 21 yourmacid.py. You have to submit to Dropbox to get a mark, posting on the gallery is not sufficient, the gallery is only for the bonus mark.Explanation / Answer
The nested function can access all the variables of the containing function that are visible at the point of its definition. This is called lexical scoping. For example, here we show a nested function which uses an inherited variable named offset:
Nested function definitions are permitted within functions in the places where variable definitions are allowed; that is, in any block, mixed with the other declarations and statements in the block.
It is possible to call the nested function from outside the scope of its name by storing its address or passing the address to another function:
Here, the function intermediate receives the address of store as an argument. If intermediate calls store, the arguments given to store are used to store into array. But this technique works only so long as the containing function (hack, in this example) does not exit.
If you try to call the nested function through its address after the containing function exits, all hell breaks loose. If you try to call it after a containing scope level exits, and if it refers to some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk. If, however, the nested function does not refer to anything that has gone out of scope, you should be safe.
GCC implements taking the address of a nested function using a technique called trampolines. This technique was described in Lexical Closures for C++ (Thomas M. Breuel, USENIX C++ Conference Proceedings, October 17-21, 1988).
A nested function can jump to a label inherited from a containing function, provided the label is explicitly declared in the containing function (see Local Labels). Such a jump returns instantly to the containing function, exiting the nested function that did the goto and any intermediate functions as well. Here is an example:
A nested function always has no linkage. Declaring one with extern or static is erroneous. If you need to declare the nested function before its definition, use auto (which is otherwise meaningless for function declarations).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.