Combination Function Help

2014-02-12 Thread 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread Mark Lawrence

On 12/02/2014 15:20, kjaku...@gmail.com wrote:

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.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread 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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread Joel Goldstick
Y
On Feb 12, 2014 11:00 AM, kjaku...@gmail.com wrote:

 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)
Following line never runs
 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
 --
 https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread John Ladasky
On Wednesday, February 12, 2014 7:56:05 AM UTC-8, kjak...@gmail.com 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 which 
return 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 it happens 
to be nested inside the int() function call.  Let's take the simpler one first.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread Mark Lawrence

On 12/02/2014 15:56, kjaku...@gmail.com wrote:

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.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread Dave Angel
 kjaku...@gmail.com Wrote in message:
 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.

-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread 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..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread Mark Lawrence

On 12/02/2014 22:59, kjaku...@gmail.com wrote:

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.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
https://mail.python.org/mailman/listinfo/python-list


Re: Combination Function Help

2014-02-12 Thread Dave Angel
 kjaku...@gmail.com Wrote in message:
 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.
 

-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list