0

So I am working on a program to create portmanteaus. I have the code and functions I need, and I have put them together.

Here is the code:

def portmanteauscore(start, mid, end):
 totallen = len(start) + len(mid) + len(end)
 return totallen - abs((len(start)/totallen) - len(start)) - abs((len(mid)/totallen) - len(mid)) - abs((len(end)/totallen) - len(end))
def portmanteaugenerator(word1, word2, words):
 mid = longest_common_substring(word1, word2)
 start = word1[:word1.index(mid)]
 end = word2[len(mid):]
 if start + mid in words and mid + end in words:
 return start, mid, end
def natalie(words):
 "Find the best Portmanteau word formed from any two of the list of words."
 wordpermutations = list(itertools.permutations(words))
 maxscore, bestnatalie = 0, ''
 for perm in wordpermutations:
 start, mid, end = portmanteaugenerator(perm[0], perm[1], words)
 if portmanteauscore(start, mid, end) > maxscore:
 bestnatalie, maxscore = start + mid + end, portmanteauscore(start, mid, end)
 print bestnatalie
 return bestnatalie
def longest_common_substring(s1, s2):
 m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
 longest, x_longest = 0, 0
 for x in xrange(1, 1 + len(s1)):
 for y in xrange(1, 1 + len(s2)):
 if s1[x - 1] == s2[y - 1]:
 m[x][y] = m[x - 1][y - 1] + 1
 if m[x][y] > longest:
 longest = m[x][y]
 x_longest = x
 else:
 m[x][y] = 0
 return s1[x_longest - longest: x_longest]

But when I run the code, I keep getting this error message,

Traceback (most recent call last):
 File "vm_main.py", line 33, in <module>
 import main
 File "/tmp/vmuser_ijxrjleuxj/main.py", line 107, in <module>
 print test_natalie()
 File "/tmp/vmuser_ijxrjleuxj/main.py", line 87, in test_natalie
assert natalie(['adolescent', 'scented', 'centennial', 'always', 'ado']) in ('adolescented','adolescentennial')
 File "/tmp/vmuser_ijxrjleuxj/main.py", line 67, in natalie
 start,mid,end=portmanteaugenerator(perm[0],perm[1],words)
 TypeError: 'NoneType' object is not iterable

This occurs when I return the start,mid,and end variables for the portmanteau generator. When given a list of words, it should return a portmanteau from two words that is the best according to portmanteau score.

But I keep getting this type error for some reason. I have tried making start,mid,end a list and it still fails to run. Can you please help me?

Talvalin
7,8972 gold badges33 silver badges40 bronze badges
asked Jul 9, 2013 at 5:03
2
  • 2
    More spaces! Spaces after commas and around binary operators would make this a lot more readable. Commented Jul 9, 2013 at 5:12
  • Ironic when talking about a portmanteaugenerator ;) Commented Jul 9, 2013 at 6:34

1 Answer 1

6

Most likely, start+mid in words and mid+end in words returns False and so the function, instead of passing through the if-statement, returns None (because if a function does not return something, it defaults to None).

Then you're trying to do something like:

start,mid,end = None

What python is trying to do is split up None in those three variables. It's like doing this:

one, two, three = (1, 2, 3)

But you can't, because None is not an iterable.

answered Jul 9, 2013 at 5:04
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.