Global and local variables although simple, but still record, mainly reproduced reprinted from: Http://blog.sina.com.cn/s/blog_436992740102ux8z.html First look at a piece of code: visible Old_price is a global variable, final_price is the result of a local variable running:
Local variables get outside. If there is no definition, then the global variable can be used in the DEF function?
Operation Result:
Visible, the DEF-defined function can print global variables if you want to make changes to global variables in functions defined by Def, you should write this:
The global declaration of Old_price in the function must be written, otherwise, when the function of the old_price is changed, the system found that the name and the global variable has a name is the same, it will be in the function of the old_price as a newly established local variable, then, The first print in the function will go wrong because there is no definition at this time that Old_price,old_price is defined by the phrase = 50. Run Result: The result of the operation is known: The value of the global variable has been changed in the function.
Python local and global variables