Combination Function Help

K

kjakupak

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.
 
M

Mark Lawrence

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.

Actually calling choices with n_input and k_input as inputs might help
your cause.
 
K

kjakupak

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)
print ("Total number of ways of choosing %d out of %d courses: " % (n, k))

n = int(input("Number of courses you like: "))
k = int(input("Number of courses you can register for: "))
choices(n, k)

Changed it like you said, didn't work
 
J

John Ladasky

On Wednesday, February 12, 2014 7:56:05 AM UTC-8, (e-mail address removed) wrote:
[snip]
choices(n, k)

Changed it like you said, didn't work

What are you doing with the value returned by the function, choices()? Right now, you aren't doing anything with it. You are throwing it away. That's the beginning of your problem.

In your own program, you have two other working examples of functions whichreturn and use values. You should study these.

The first, and easier function call for you to understand is the call to int(). You call it twice. The first time, you ASSIGN the name "n" to the value returned by int(). The second time, you assign the name "k" to the value returned by another run of int().

Assigning the names to the values returned by the function calls is what allows you to work with those values later, on other lines of your program. Without assigning n and k, attempting to call choices(n,k) would generate an error.

If int() was written in Python (it probably isn't; most of the Python core is written in C) and you looked at int()'s source code, it would have a "return" statement in it, just like your choices() does.

Does that help you to see how you should modify the last line of your program, and what you might do after that?

There's a second function call in your program as well: you call input(). This function call would be harder for a beginner to understand, because ithappens to be nested inside the int() function call. Let's take the simpler one first.
 
M

Mark Lawrence

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)
print ("Total number of ways of choosing %d out of %d courses: " % (n, k))

n = int(input("Number of courses you like: "))
k = int(input("Number of courses you can register for: "))
choices(n, k)

Changed it like you said, didn't work

Changed it like who said? I'm assuming myself, but with no context you
can't always tell. Also, stating "didn't work" is often useless to us.
What didn't work? Why didn't it work? Here it's obvious, you're
throwing away the return value from your function call. Either save the
return value and print it or add the function call directly to your
original print call.
 
D

Dave Angel

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)
print ("Total number of ways of choosing %d out of %d courses: " % (n, k))

n = int(input("Number of courses you like: "))
k = int(input("Number of courses you can register for: "))
choices(n, k)

Changed it like you said, didn't work

I see at least two problems with that code
:

The line with the print function will never get called, since it
follows an unconditional return statement. You shouldn't print
there anyway, just move it to top level, after the two calls to
input. Don't forget to dedent it.

You don't use or save the return value of choices. You should
probably assign it to a name like combinations, then print it on
the following line.

The recursive function choices doesn't look right to me, but I'm
not sure either way. I have not tested it.
 
K

kjakupak

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)

comb = choices(n, k)
print comb

print ("Total number of ways of choosing %d out of %d courses: " % (n, k))
n = int(input("Number of courses you like: "))
k = int(input("Number of courses you can register for: "))

Still isn't working though..
 
M

Mark Lawrence

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)

comb = choices(n, k)
print comb

print ("Total number of ways of choosing %d out of %d courses: " % (n, k))
n = int(input("Number of courses you like: "))
k = int(input("Number of courses you can register for: "))

Still isn't working though..

Please be precise, "still isn't working" tells us nothing.
 
D

Dave Angel

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)

comb = choices(n, k)
print comb

print ("Total number of ways of choosing %d out of %d courses: " % (n, k))
n = int(input("Number of courses you like: "))
k = int(input("Number of courses you can register for: "))

Still isn't working though..

Still haven't figured how to post a traceback?

The two lines defining and referencing comb need to be AFTER n and
k are defined and given values.

Also, what version of Python are you running? Version 3.x
requires parentheses around the arguments to the print function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top