fork download
  1. def createCounter():
  2. i = 0
  3. def counter():
  4. nonlocal i
  5. i += 1
  6. return i
  7. return counter
  8.  
  9. # 测试:
  10. counterA = createCounter()
  11. print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
  12. counterB = createCounter()
  13. if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
  14. print('测试通过!')
  15. else:
  16. print('测试失败!')
  17.  
  18. # your code goes here
  19. # your code goes here
Success #stdin #stdout 0.08s 14228KB
stdin
Standard input is empty
stdout
1 2 3 4 5
测试通过!