0
def factorial(n):
    return 1 if (n==1 or n==0) else n * factorial(n - 1)

def perm(n, r):
    nFactorial = factorial(n)
    midValue = n - r
    midFactorial = factorial(midValue)
    answer = nFactorial / midFactorial
    return answer

def comb(n, r):
    perm = perm(n, r)
    rFactorial = factorial(r)
    answer = perm / rFactorial
    return answer

The error message says "UnboundLocalError: local variable 'perm' referenced before assignment".

  • 2
    First line in `def comb`. Your `perm` variable has the same name as your `perm` function. – thebjorn Jan 08 '23 at 12:56
  • 2
    `perm = perm(n, r)` you are overriding your function definition of perm. – phibel Jan 08 '23 at 12:57
  • You should also be using `//` for integer division rather than `/`. Note also that your functions are inefficient. There is no good reason to compute two large factorials just to cancel many or even most of the factors. – John Coleman Jan 08 '23 at 13:04

0 Answers0