我是Python初学者。我写了类似如下的代码只是为了练习..请看看它为什么全局变量不能在Python的不同函数中工作?
i=1
def wrte():
global i
while i<5:
print "%s .Line..\n" %i
i+=1
def appnd():
j=i
while i in range(i,j+3):
print "%s .Line..\n" %i
i+=1
def Main():
wrte()
appnd()
Main()
输出类似如下
1 .Line..
2 .Line..
3 .Line..
4 .Line..
**Traceback (most recent call last):
Line 18, in <module>
Main()
Line 16, in Main
appnd()
Line 9, in appnd
j=i
UnboundLocalError: local variable 'i' referenced before assignment**
预期结果::接下来的顺序应该附加像
5. Line..
6. Line..
7. Line..
请帮我在这..
这是一个在'wrte',想想你在做什么正确的,使其工作... – 2012-04-17 09:06:44