473,581 Members | 5,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TypeError, I know why but not how!?

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 None? I
mean I know why the program aborts but I dont understand why the None
is generated all the sudden. Hitting recursion depth isn't reported
with that error.
Guess I am missing something very obvious.

Traceback (most recent call last):
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 34, in <module>
gen(50)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 32, in gen
return generateList([], [(1,10),(4,272), (34,442),(112,4 2)], [],
rooms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 27, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 27, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 27, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 27, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 27, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 25, in generateList
room, placed = pair(incompatib les, placed)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 12, in pair
student1, placed = validate(placed )
TypeError: 'NoneType' object is not iterable
>>>
----------------------------------
the program:

import random

def validate(placed ):
student = round(random.ra ndom()*401)
if student in placed:
validate(placed )
else:
placed.append(s tudent)
return student, placed

def pair(incompatib les, placed):
student1, placed = validate(placed )
student2, placed = validate(placed )
pair1 = (student1,stude nt2)
pair2 = (student2,stude nt1)
if (pair1 or pair2) in incompatibles:
placed.remove(s tudent1)
placed.remove(s tudent2)
pair(incompatib les, placed)
else:
return pair1, placed

def generateList(do rmlist,incompat ibles, placed, rooms):
if len(dormlist) < (rooms + 1):
room, placed = pair(incompatib les, placed)
dormlist.append (room)
generateList(do rmlist,incompat ibles,placed,ro oms)
else:
return dormlist

def gen(rooms):
return generateList([], [(1,10),(4,272), (34,442),(112,4 2)], [],
rooms)

gen(50)
-------------------------------------------------------------------------------------------------------------------
some tests inserted:
import random

def validate(placed ):
student = round(random.ra ndom()*401)
if student in placed:
validate(placed )
else:
placed.append(s tudent)
return student, placed

def pair(incompatib les, placed):
x = validate(placed )
print "x",x
y = validate(placed )
print "y",y
print "-------------------------------------"
student1, placed = x
student2, placed = y
#student1, placed = validate(placed )
#student2, placed = validate(placed )
pair1 = (student1,stude nt2)
pair2 = (student2,stude nt1)
if (pair1 or pair2) in incompatibles:
placed.remove(s tudent1)
placed.remove(s tudent2)
pair(incompatib les, placed)
else:
return pair1, placed

def generateList(do rmlist,incompat ibles, placed, rooms):
## print dormlist
## print placed
## print "---------------------------------------------------"
if len(dormlist) < (rooms + 1):
room, placed = pair(incompatib les, placed)
dormlist.append (room)
generateList(do rmlist,incompat ibles,placed,ro oms)
else:
return dormlist

def gen(rooms):
return generateList([], [(1,10),(4,272), (34,442),(112,4 2)], [],
rooms)

gen(50)

------------------------
x (283.0, [283.0])
y (8.0, [283.0, 8.0])
-------------------------------------
x (359.0, [283.0, 8.0, 359.0])
y (158.0, [283.0, 8.0, 359.0, 158.0])
-------------------------------------
x (249.0, [283.0, 8.0, 359.0, 158.0, 249.0])
y (371.0, [283.0, 8.0, 359.0, 158.0, 249.0, 371.0])
-------------------------------------
x (199.0, [283.0, 8.0, 359.0, 158.0, 249.0, 371.0, 199.0])
y (292.0, [283.0, 8.0, 359.0, 158.0, 249.0, 371.0, 199.0, 292.0])
-------------------------------------
x None
y (227.0, [283.0, 8.0, 359.0, 158.0, 249.0, 371.0, 199.0, 292.0, 47.0,
227.0])
-------------------------------------

Traceback (most recent call last):
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 44, in <module>
gen(50)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 42, in gen
return generateList([], [(1,10),(4,272), (34,442),(112,4 2)], [],
rooms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 37, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 37, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 37, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 37, in generateList
generateList(do rmlist,incompat ibles,placed,ro oms)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 35, in generateList
room, placed = pair(incompatib les, placed)
File "C:\Python25\Pr ogs\PNP\pnp.py" , line 17, in pair
student1, placed = x
TypeError: 'NoneType' object is not iterable
Jul 10 '08 #1
6 1483
ssecorp wrote:
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 None? I
mean I know why the program aborts but I dont understand why the None
is generated all the sudden. Hitting recursion depth isn't reported
with that error.
def validate(placed ):
student = round(random.ra ndom()*401)
if student in placed:
# You need to explicitly return, here:
return validate(placed )
else:
placed.append(s tudent)
return student, placed

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 10 '08 #2
On 2008-07-10, ssecorp <ci**********@g mail.comwrote:
>
def validate(placed ):
student = round(random.ra ndom()*401)
if student in placed:
validate(placed )
else:
placed.append(s tudent)
return student, placed

def pair(incompatib les, placed):
student1, placed = validate(placed )
student2, placed = validate(placed )
pair1 = (student1,stude nt2)
pair2 = (student2,stude nt1)
if (pair1 or pair2) in incompatibles:
placed.remove(s tudent1)
placed.remove(s tudent2)
pair(incompatib les, placed)
else:
return pair1, placed

def generateList(do rmlist,incompat ibles, placed, rooms):
if len(dormlist) < (rooms + 1):
room, placed = pair(incompatib les, placed)
dormlist.append (room)
generateList(do rmlist,incompat ibles,placed,ro oms)
else:
return dormlist
In each of the above functions, the 'if' part has no 'return', while the 'else'
part has. You should add a 'return' statement at every exit from the function.
If you don't expect the program to get at some point, you should add a check
like

raise ValueError("Thi s shouldn't happen")

you'd be surprised how often 'impossible' things do happen (and writing such a
statement costs less than a minute, and saves you many hours debugging).
Some people so as far as demanding that there is exactly 1 return statement,
namely at the bottom of the function. While I don't favor that approach, it may
help you in always returning a value.

BTW: For performance and readability, you may want to replace your direct
recursion with iterations.
Albert
Jul 10 '08 #3
ty I came to the same conckusion in bed :)

now it works.
however since there are 400 students and some are incompatible I
shouldnt be able to generate a 200room list right?

but it works sometimes the other times i get an error. might be
because of recursion depth i never let the error finish.
i saw i also generate student 0 which shouldnt exist.


and the actual problem is generating all possible combinations right?
i mean i can easily generate one list that is made randomly so problem
solved.

why does one need to generate all possible lists? i guess there are
other problems where that might be needed so is this just a bad
example or am i missing something?
Jul 10 '08 #4
On Jul 10, 12:07 pm, ssecorp <circularf...@g mail.comwrote:
pair1 = (student1,stude nt2)
pair2 = (student2,stude nt1)
if (pair1 or pair2) in incompatibles:
Apart from the problems that others have mentioned, the above
statement is NOT doing what you think it is. (pair1 or pair2) will
always produce pair1, because pair1, a tuple of length 2, cannot be
false. So the statement is equivalent to:
if pair1 in incompatibles:
What you want is:
if pair1 in incompatibles or pair2 in incompatibles:

Cheers,
John
Jul 10 '08 #5
I don't fully understand why I have to do this.
On Jul 10, 4:17*am, Robert Kern <robert.k...@gm ail.comwrote:
ssecorp wrote:
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 None? I
mean I know why the program aborts but I dont understand why the None
is generated all the sudden. Hitting recursion depth isn't reported
with that error.

def validate(placed ):
* * *student = round(random.ra ndom()*401)
* * *if student in placed:
* * * * *# You need to explicitly return, here:
* * * * *return validate(placed )
* * *else:
* * * * *placed.append( student)
* * * * *return student, placed

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
* that is made terrible by our own mad attempt to interpret it as though it had
* an underlying truth."
* *-- Umberto Eco
Jul 10 '08 #6
On Jul 10, 11:15*pm, ssecorp <circularf...@g mail.comwrote:
I don't fully understand why I have to do this.
Because your function is recursive.

You call your function passing it a list of numbers. It generates a
new number and looks to see if it's in the 'placed' list. If it is, it
calls itself again, but if it isn't, it returns the number & an
updated 'placed'.

Without placing a return before the recursive call to validate, it
calls validate in place, ignores its return result, then drops out of
the conditional to the end of the function, where an implicit 'return
None' occurs.

By returning what validate passes back, you in effect pass it back up
the chain of recursive calls, so the final return will _always_ pass
back the number, list combo you require.

- alex23
Jul 11 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13071
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 statements calling the class objects in my extention DLL. 3. Before each execution I import my extention module using;
7
3960
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2 arguments, got 0". Tested with Python 2.3.4 on OpenBSD and Python 2.4 on Win98; same results. Is this a bug in deepcopy, how I dynamically assign the...
6
2202
by: Mike Brown | last post by:
This works as expected (this is on an ASCII terminal): >>> unicode('asdf\xff', errors='replace') u'asdf\ufffd' This does not work as I expect it to: >>> class C: .... def __str__(self):
1
2170
by: Paul Du Bois | last post by:
Using win32 python 2.4.1, I have a minimal test program: def generate(): raise TypeError('blah') yield "" print "\n".join((generate())) Executing the program gives:
42
3423
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
7
12713
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 != indexList1.pop(): index1+=1 index2+=1 continue
1
5897
by: OtisUsenet | last post by:
Hi, I have a bookmarklet that works perfectly in Firefox, IE, Konqueror, and Opera, but in Safari 2.0.3 (417.9.2) it doesn't work. I enabled debugging and I can see "TypeError - Undefined value" reported in the Javascript console, but I'm not sure where the error is. The bookmarklet in question is here: javascript:if...
8
9509
by: Gilbert Fine | last post by:
This is a very strange exception raised from somewhere in our program. I have no idea how this happen. And don't know how to reproduce. It just occurs from time to time. Can anyone give me some suggestion to fix this? Thanks. -- Gilbert
10
7710
by: est | last post by:
>>import md5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File "C:\Python25\lib\pickle.py", line 224, in dump self.save(obj) File "C:\Python25\lib\pickle.py", line 306, in save rv = reduce(self.proto) File...
0
7854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8134
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8157
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6539
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1394
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.