NameError: global name 'reduce' is not defined

前端 未结 2 1245
北荒
北荒 2020-12-17 08:27

I\'m new to Python. Would you please tell me what\'s wrong with the following code? When I run it, I got an error message of \"NameError: global name \'reduce\' is not defin

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 08:53

    Also

    # Import reduce from functools
    from functools import reduce
    

    Allows you to use reduce as though it were a built in function.

    def main():
        def add(x,y): return x+y
        reduce(add, range(1, 11))
    

提交回复
热议问题