Discuss / Python / 作业,照抄的: )

作业,照抄的: )

Topic source

def createCounter(): f = [0] def counter(): f[0] = f[0] + 1 return f[0] return counter

为什么用f[0] 我试了一下用个变量num居然报错。。为什么?

西瓜太郎1

#3 Created at ... [Delete] [Delete and Lock User]

#这样写应该不会报错。

def createCounter(): num = 0 def counter(): nonlocal num num = num +1 return num return counter


  • 1

Reply