Discuss / Python / 返回函数练习的提问

返回函数练习的提问

Topic source

蘭__吾凡

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

def createCounter():

    def counter():

        i = [0]

        def h():

            i = i + 1

            return i

        return h

    return counter

这个为什么不行呢

你的i是list

这里:

def h():

            i = i + 1

            return i

你想做的是list里唯一的元素加一,应该改为i[0] + 1

而且,每一次执行函数,你的i都有被重新赋值 i = [0],这个,应该不是你想看到的


  • 1

Reply