返回值与已定义函数的预测值不同

时间:2019-05-26 13:07:20

标签: python-3.7

^# [^#\n\r]+$

james=open('C:/Users/skora da_bura/Documents/data.txt') jake=james.read() james.close() numblist=[] charlist=[] def Read(numblist,charlist): for i in range(0,len(jake),4): numblist.append(int(jake[i])) for i in range(2,len(jake),4): charlist.append(jake[i]) Bring = numblist,charlist james = open('C:/Users/skora da_bura/Documents/data.txt') jake22 = james.readlines() james.close() back='Number of lines read',len(jake22) return back print(Read([],[])) print(charlist) 返回[],即使我在定义函数Read时将附加值添加到列表中也是如此。 我似乎看不出代码有什么问题

1 个答案:

答案 0 :(得分:0)

您在#this is not a comment (it is exported as "#this is not a comment") 签名中定义的charlist遮盖了全局Read。它们是碰巧具有相同名称的不同变量。如果您打算修改全局变量,则不应尝试将其作为参数传递。