Python core programming version 2, 308th page, Chapter 4 exercises continued 5-answers to Python core programming-self-developed-

Source: Internet
Author: User

The answer in this blog is not from official resources, but from my own exercises. If you have any questions or errors, please discuss them.

11-18.
Synchronous function call. Review the situation of the husband and wife mentioned when introducing the shortest copy and deep copy in chapter 1 (Conclusion 6th ). They share a common account and adversely affect access to their bank account. Create a program so that the function called to change the account balance must be synchronized.
[Unfinished]
It is difficult to answer this question.

 

11-19.

Variable Scope. Earlier in the chapter (see Example 11.9 on p. 466), we left determining the output of scope. py as an exercise for the reader.
(A) Write down your best guess, then download the code and run it. Were you close? Explain where you were wrong and why (if applicable). Hint: first determine the total number of lines output by counting how statements print statements will execute before trying to figure out the output.
(B) Line 11 in proc2 () is currently blank... insert this statement (indented properly): global j. How does this affect the output (and why )?
[Note] the original English version has this question. Example 11.9 refers to the Example in section 11.8.6 of the Chinese version.
[Answer]
(A) running result

j == 3 and k == 4j == 1 and k == 7j == 3 and k == 4j == 6 and k == 7j == 8 and k == 7

 

(B) running result

j == 3 and k == 4j == 1 and k == 7j == 3 and k == 4j == 6 and k == 7j == 6 and k == 7
 
# This article is from balian
*** *** *** ***

[Example source code]

Example 11.9 Variable Scope (scope. py)

 

j, k = 1, 2def proc1():    j, k = 3, 4    print "j == %d and k == %d" % (j, k)    k = 5def proc2():    #global j    j = 6    proc1()    print "j == %d and k == %d" % (j, k)k = 7proc1()print "j == %d and k == %d" % (j, k)j = 8proc2()print "j == %d and k == %d" % (j, k)

 

***   ***   ***   ***

Key word Python core programming answers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.