Combination Function Help

kjakupak at gmail.com kjakupak at gmail.com
Wed Feb 12 16:20:32 CET 2014


So I need to write a function based off of nCr, which I have here:

def choices(n, k):
    if n == k:
        return 1
    if k == 1:
        return n
    if k == 0:
        return 1
    return choices(n - 1, k) + choices(n - 1, k - 1)

It works fine, but then I need to add in so that the user can have an input box for n and k.

def choices(n, k):
    if k == 1:
        return n
    if n == k:
        return 1
    if k == 0:
        return 1
    return choices(n - 1, k) + choices(n - 1, k - 1)

n_input = int(input("Number of courses you like: "))
k_input = int(input("Number of courses you can register for: "))

The above doesn't return any value at all. And then I need it to print the total number of ways of choosing k out of n courses, which I only have:

print ("Total number of ways of choosing %d out of %d courses: " % (n_input, k_input))


So basically I need help figuring out why it won't return any value and how to print that final value.



More information about the Python-list mailing list