我在网上发现,当函数调用完成时,函数的局部变量不能从外部访问。我尝试执行程序,但它抛出一个错误,变量没有定义。我的代码是
xyz=list()
n=0
def length(g):
i=0
n=g
v=input("no of")
while i<v:
c=input("Enter the 1st dimension:")
j=input("Enter the 2nd dimension:")
i=i+1
xyz.append(c)
xyz.append(j)
return c
return j
return n
def prod():
global c
for i in xyz:
if n<c and n<j:
print "Upload another"
elif n==c and n==j:
print "Accepted"
else:
print "Crop it"
length(input("ENter the length"))
prod()
print xyz
抛出如下错误
Traceback(最近一次调用):文件"C:Python27pic.py",第32行,在刺激()文件"C:Python27pic.py",第21行,在prod如果n
我猜这就是你想要做的
xyz=list()
n=0
def length(g):
i=0
n=g
v=input("no of")
global xyz
while i<v:
c=input("Enter the 1st dimension:")
j=input("Enter the 2nd dimension:")
i=i+1
xyz.append(c)
xyz.append(j)
return c,j,n
def prod():
global xyz
c,j,n = length(input("ENter the length"))
for i in xyz:
if n<c and n<j:
print "Upload another"
elif n==c and n==j:
print "Accepted"
else:
print "Crop it"
prod()
print xyz
输出ENter the length 2
no of 2
Enter the 1st dimension: 1
Enter the 2nd dimension: 2
Crop it
Crop it
[1, 2]