TypeError in python - missing 1 required positional argument

后端 未结 3 1423
遥遥无期
遥遥无期 2021-01-28 09:31

I\'m stuck here. For n = 5 and k = 3, the answer should be 19. If I set k = 3 separately as a local or global variable and ru

3条回答
  •  醉梦人生
    2021-01-28 09:55

    Your wabbits() function takes two arguments:

    def wabbits(n, k):
    #           1  2
    

    but your code calls it with just one:

    return wabbits(n-2)*k + wabbits(n-1)
    #              ^^^              ^^^^
    

    You need to pass in a value for k as well. You could just pass in the current value of k:

    def wabbits(n, k):
        if n == 1:
            return 1
        if n == 2:
            return 1
        return wabbits(n-2, k)*k + wabbits(n-1, k)
        #              1    2              1    2
    

    and indeed that produces 19:

    >>> def wabbits(n, k):
    ...     if n == 1:
    ...         return 1
    ...     if n == 2:
    ...         return 1
    ...     return wabbits(n-2, k)*k + wabbits(n-1, k)
    ...
    >>> wabbits(5, 3)
    19
    

提交回复
热议问题