Python 练习实例42

python 练习实例42

python 编程100例python 编程100例

题目:学习使用auto定义变量的用法。

程序分析:没有auto关键字,使用变量作用域来举例吧。

程序源代码:

实例:

#!/usr/bin/python
# -*- coding: utf-8 -*-

num = 2
def autofunc():
    num = 1
    print ('internal block num = %d' % num)
    num += 1
for i in range(3):
    print ('the num = %d' % num)
    num += 1
    autofunc()

以上实例输出结果为:

the num = 2
internal block num = 1
the num = 3
internal block num = 1
the num = 4
internal block num = 1

python 编程100例python 编程100例

下一节:python 练习实例43

python 编程100例

相关文章