469,290 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

TypeError: unsupported operand type(s) for /: 'NoneType' and'NoneType'

I'm trying to write a simple program to calculate permutations. I created a file called "mod.py" and put the following in it:

def factorial(n):
a = n
b = n
while a>0 and b>1:
n = (n)*(b-1)
b = b-1

def perm(n, r):
a = factorial(n)
b = factorial(n-r)
q = a / b
print q

Then I went back to IDLE and input the following:
>>import mod
mod.perm(5, 4)
I recieved the following error message:

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
mod.perm(5, 4)
File "C:\Python25\mod.py", line 27, in perm
q = a / b
TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType'

I have no idea how to fix it. I'm pretty new to Python. I have IDLE 1.2.2 and Python 2.5.2,

Please help.
Jun 27 '08 #1
2 7244
On May 1, 5:21*pm, "Jordan Harry" <jha...@EmeryWeiner.orgwrote:
I'm trying to write a simple program to calculate permutations. *I created a file called "mod.py" and put the following in it:

def factorial(n):
* * a = n
* * b = n
* * while a>0 and b>1:
* * * * n = (n)*(b-1)
* * * * b = b-1

def perm(n, r):
* * a = factorial(n)
* * b = factorial(n-r)
* * q = a / b
* * print q

Then I went back to IDLE and input the following:
>import mod
mod.perm(5, 4)

I recieved the following error message:

Traceback (most recent call last):
* File "<pyshell#1>", line 1, in <module>
* * mod.perm(5, 4)
* File "C:\Python25\mod.py", line 27, in perm
* * q = a / b
TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType'

I have no idea how to fix it. *I'm pretty new to Python. *I have IDLE 1.2.2 and Python 2.5.2,

Please help.
Your factorial function needs to return something.

~Sean
Jun 27 '08 #2
"Jordan Harry" <jh****@EmeryWeiner.orgwrites:
I'm trying to write a simple program to calculate permutations. I
created a file called "mod.py" and put the following in it:

def factorial(n):
a = n
b = n
while a>0 and b>1:
n = (n)*(b-1)
b = b-1
A function that does some calculations internally, but has no 'return'
statement and thus implicitly returns 'None'.
def perm(n, r):
a = factorial(n)
b = factorial(n-r)
These statements bind the 'None' returned by the 'factorial' function
to the names 'a' and 'b'.
q = a / b
This attempts to use the '/' operator on 'None' with 'None', which
raises the exception you saw.

(good sigmonster, have a cookie)

--
\ "Pinky, are you pondering what I'm pondering?" "I think so, |
`\ Brain, but Zero Mostel times anything will still give you Zero |
_o__) Mostel." -- _Pinky and The Brain_ |
Ben Finney
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Atul Kshirsagar | last post: by
16 posts views Thread by gaudetteje | last post: by
1 post views Thread by homepricemaps | last post: by
reply views Thread by John Allman | last post: by
9 posts views Thread by thompson.marisa | last post: by
5 posts views Thread by tom | last post: by
2 posts views Thread by Gabriel Genellina | last post: by
6 posts views Thread by ssecorp | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.