0

Why is this returning an error when I try to call this function recursively in python3?

class Solution:
    def climbStairs(self, n: int) -> int:
        if n == 2:
            return 2 
        if n == 3:
            return 3
        else:
            return climbStairs(self, n - 1) + climbStairs(self, n - 2)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Welcome to Stack Overflow. How do you try to call it? What error do you get? Please read [ask] and [mre], and show a [complete](https://meta.stackoverflow.com/questions/359146) error message. – Karl Knechtel Oct 05 '22 at 23:58
  • BTW, welcome to Stack Overflow! Please take the [tour]. – wjandrea Oct 06 '22 at 00:01
  • Are we supposed to guess what the error is? – John Gordon Oct 06 '22 at 00:02
  • @John Isn't it obvious? `NameError: name 'climbStairs' is not defined`. That said, OP *should* provide it. – wjandrea Oct 06 '22 at 00:03
  • @wjandrea It could be that, or it could be the wrong number of arguments, or it could be something about the class instantiation (which was not shown), or or or ... – John Gordon Oct 06 '22 at 00:06
  • @John Ah yeah, true, but this looks like a LeetCode challenge based on the `class Solution`, and IIRC, it takes care of that stuff for you. – wjandrea Oct 06 '22 at 00:08

1 Answers1

0

is this a method (defined inside a class)? If so:

class Your Class:
   def climbStairs(self, n: int) -> int:
       if n == 2:
           return 2 
       if n == 3:
           return 3
       else:
           return self.climbStairs(n - 1) + self.climbStairs(n - 2)

if it is not a method, it is a function

def climbStairs(n: int) -> int:
    if n == 2:
        return 2 
    if n == 3:
        return 3
    else:
        return climbStairs(n - 1) + climbStairs(n - 2)