List problem

Jon Cosby jcosby at mindspring.com
Fri Mar 29 16:34:29 EST 2002


Can anyone se why this is coming up empty in the "cols" list? I've tried
something similar in the interpreter, and everything here looks right. For
some reason, it isn't finding any matches here.

########################################################################
# WordSquare
# Build word squares from initial word
#


# Word dictionary
dict = "c:\data\dict.txt"

firstword = input("Enter first word (enclosed in quotes): ")
lword = len(firstword)
words = []
cols = []
rows = []


f = open(dict, "r")
for line in f.readlines():
 if len(line[:-1]) == lword:
  words.append(line[:-1])
f.close()


for i in range(lword):
 cols.append([])
 rows.append([])
rows[0].append(firstword)

# Generate an array of words with matching first letters
for i in range(lword):
 for word in words:
   if word[0] == firstword[i]: # Matches not found
   cols[i].append(word)

print len(words)
print cols

######################################################################

C:\Python21>python projects\wordsquare.py
Enter first word (enclosed in quotes): "hello"
4220
[[], [], [], [], []]

C:\Python21>



Jon Cosby






More information about the Python-list mailing list