def createCounter(): i=0 def f(): nonlocal i i += 1 return i return f # 测试: counterA = createCounter() print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5 counterB = createCounter() if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]: print('测试通过!') else: print('测试失败!')
Sign in to make a reply
萌萌小喵姐