473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 8375
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
3
by: Martin Koekenberg | last post by:
Hello, Ik get the following error when I install a new Python software package such as Zope or PIL. This is what I get whem I 'make' Zope : running build_ext Traceback (most recent call...
16
by: gaudetteje | last post by:
I just read in the 'What's New in Python 2.4' document that the None data type was converted to a constant: http://python.org/doc/2.4/whatsnew/node15.html """ # None is now a constant; code...
1
by: homepricemaps | last post by:
if i do the following i get the url of an image i am looking for image = "" image = bs.img print image however if i do this out.write (image ) i get an error that says "nonetype error is...
0
by: John Allman | last post by:
Hi all, I'm trying to create a setup which allows a program to request an object using strings and get an object of that type. It appears to be mostly working but i have difficulties if i...
9
by: thompson.marisa | last post by:
Hi. I'm extremely new to Python and programming as a whole. I have written a python script with the assistance of ESRI ArcGIS 9.2, which uses Python 2.4.1, however, it gives me this error when...
5
by: tom | last post by:
Hi! My code is And it gives following error: How can sortedList variable turn into NoneType? I just don't get it...
2
by: Gabriel Genellina | last post by:
En Sun, 15 Jun 2008 05:35:18 -0300, Maryam Saeedi <ma.saeedi@gmail.comescribió: NoneType is not an exception, but the type of the None object. Perhaps you're not interpreting correctly some...
6
by: ssecorp | last post by:
Im looking into PvsNP: http://www.claymath.org/millennium/P_vs_NP/ so I thought I'd write the program just to get a feel for it. But I run into a problem. Why does it all the sudden return...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.