predator78 22 Junior Poster

ZZucker could you explain to me where or how the variables like %d are getting there information from I have never used that type of variable before.

predator78 22 Junior Poster

Thanks for pointing out the set module I just did some research on it and its great. Seems strange to me though that sort was acting the way it was, although I notice by looking at your code I need to work on my grammer as I put it in another post. I'm workin on it!!! Anyways I think I'll modify the program to use set instead of sort for now, but I wanna find out why sort not working correctly, so I know how to use it in the future. Oh and thanks for the hint on appending the lists to Gribouillis.

predator78 22 Junior Poster

Yeah I did remove the appends from the loop and that output I posted is just showing that the lists seem to be giving a double output on item 3 reggie thus screwing the indexing up tward the bottom of the code, but it shouldn't be the way its coded. Line 13 in the output should never happen, and I think that is why the rest of it don't work right either. If anyone can tell me why it is happening or come up with some type of coding to stop it from doing that would be very helpful.

predator78 22 Junior Poster

O.K. got that and seems reasonable enough, but still getting this output from my p variable first call from before goes into the lists secound after the output of the lists.

0
0
larson, 
1
1
digital1, 
2
2
apache, 
3
3
reggie, 
3
reggie, 
4
4
joanna, 
5
5
arthur, 
6
6
saturn, 
7
7
ncc1701, 
8
8
9
9
angela

As you can see when it gets to reggie for some reason there is one extra ouput which throws the whole thing out of whack!!! It really shouldn't do that but still is.

predator78 22 Junior Poster

Hey guys I finally got my word unscrambler working the other day, now I fire it up with same code same everything and for some reason getting double output from a list that shouldn't be. So if someone could either look this over and find a problem or maybe do a test run on their computer and let me know if something is currupt somehow on my system that would be great to. I just can't tell if its the code or my computer or the interprater that is messed up. I'll attach some sample files to use to run it. Thanks again.

Here is the code.

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
thelist = []
myvar = -1
c = -1
p = -1
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')
#add file contents to lists.
while p < 9:
    i = -1
    p = p + 1
    print p
    for line in wordlistfile:
        wordlist.append(line.strip())
    for line in possiblefile:
        possible.append(line.strip())
#match length of words to narrow possible matches
    for line in wordlist:
        i = i + 1
        if len(wordlist[i]) == len(possible[p]):
            matches.append(wordlist[i])
#narrow further by checking for instances of charecters
    word = possible[p]
    x = len(matches)
    while c < x:
        c = c + 1
        for letter in word:
            if c == x:
                break
            if not letter in matches[c]:
                del matches[c]
                x = x - 1
#start …
predator78 22 Junior Poster

OK just figured it out, the while loop was running one to many times thus the last time throwing it out of index. Here is my revised code to handle it.

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
wordlist = []
i = -1
c = -1
p = 0
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')

#add file contents to lists.
for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())
#match length of words to narrow possible matches
for line in wordlist:
    i = i + 1
    if len(wordlist[i]) == len(possible[p]):
        matches.append(wordlist[i])
#narrow further by checking for instances of charecters
word = possible[p]
x = len(matches)
while c < x:
    c = c + 1
    for letter in word:
        if c == x:
            break
        if not letter in matches[c]:
            del matches[c]
            x = x - 1
predator78 22 Junior Poster

Not sure if this helps or not either but when I print out c or x after the del statement they merge at around the value of 40 and that seems to be when c goes out of the range of the index.

predator78 22 Junior Poster

OK I modified the if statement, but when I tried the matches.remove its trying to remove the letter instead of the list item. If I use use del matches its taking that item out of the list thus making the list smaller, but you are right the length stays the same in the while statement, which I thought it would update it every time it runs the while loop. Is there a way to modify the code so that the index can be updated every time through the loop?

predator78 22 Junior Poster

Hey guys, sorry I thought I had enough of the code to make it understandable what was goin on. Here is the rest of the code. I'm trying to run through the list to check if the two words have the same charecters in them. If they don't I want it to delete that entry from the list. Thanks a bunch.

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
wordlist = []
i = -1
c = -1
p = 0
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')

#add file contents to lists.
for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())
#match length of words to narrow possible matches
for line in wordlist:
    i = i + 1
    if len(wordlist[i]) == len(possible[p]):
        matches.append(wordlist[i])
#narrow further by checking for instances of charecters
word = possible[p]
x = len(matches)
while c < x:
    c = c + 1
    print len(matches)
    print c
    for letter in word:
        if letter in matches[c]:
            nothing = 0
        else:
            del matches[c]
predator78 22 Junior Poster

Hey all, still working on my lil program. But now that I'm getting into loops I'm pretty confused on this one. Needless to say I think this is the first while loop I have used since I started python about a week ago. What I'm trying to do at this point is narrow the possibilities down by checking the list for an instance of charecters in the scrambled word. Well I thought this would run through the list and check each item, but the problem is the index seems to be going out of range, I thought because as it is deleting items there is no update to the variable. So I tried putting something like c = c - 1 after deleting but I think this made it worse after printing the index number after and getting negative results. I assume it has something to do with the for loop its wrapped inside, but not sure. Anyway this is the code any help or suggestions would be awsome, thanks btw for all the help so far.

word = possible[p]
x = len(matches)
while c < x:
    c = c + 1
    print len(matches)
    print c
    for letter in word:
        if letter in matches[c]:
            nothing = 0
        else:
            del matches[c]
predator78 22 Junior Poster

I see thanks, that is one of the few items in the library that I can actually understand, lol. I'm not very good with the terminology so it always sounds so cryptic in the definitions.;)

predator78 22 Junior Poster

Thanks works like a charm. What exactly does strip() do though? Just wondering.

predator78 22 Junior Poster

Hey guys,
I been working on a small project I found on another forum. It's supposed to decode scrambled word from a word list in 30 sec or less. Well I'm pretty noobish so my method might not be all that great, but I'm giving it a go and learning quite a bit along the way. Anyways I figured out how to get the information from the files and put it into lists, but the problem I'm facing now is that the copied lists carry over the \n with them, which I need to get rid of. Any ideas or suggestions would be awsome thanks.

here is the code:

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
wordlist = []
i = -1
p = 0
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')

#add file contents to lists.
for line in wordlistfile:
    wordlist.append(line)
for line in possiblefile:
    possible.append(line)

for line in wordlist:
    i = i + 1
    if len(wordlist[i]) == len(possible[p]):
        matches.append(wordlist[i])
print matches

        
    

wordlistfile.close()
possiblefile.close()
predator78 22 Junior Poster

Hahaha thanx a bunch. Looks like I need to work on my grammer quite a bit. :)

predator78 22 Junior Poster

Ok well I made those changes in the code that were kinda screwy, but now the client side hangs instead of printing the message at the end of it. Any ideas?


here is the new code

for server

from socket import *
HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print'waiting for connection...'
    clientsock, addr = serversock.accept()
    print'Connection made from: ', addr

    while 1:
        data =clientsock.recv(BUFSIZ)
        if not data:
            break
    clientsock.sendall('echoed' + data)

    clientsock.close()
serversock.close()

and for client

from socket import *

HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while 1:
    data =raw_input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(1024)
    if not data:
        break
    print data
tcpCliSock.close()
predator78 22 Junior Poster

Hello all,
I'm new to the forum and also to python :D. Anyway I have been enjoying it quite a bit and wanted to start into something a bit more technical. So I though sockets would be a good start. I found a great sight , but the first script I found there seems to have some errors.
Could someone plz look this over and fix it for me? And also its been along time since I tackled programming but isn't there like a system pause or something I can stop scripts at a certain point with. If so can someone help refresh my memory. Thanx alot and diggin this sight.:)

code for server

from socket import *
HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print'waiting for connection...'
    clientsock, addr = serversock.accept()
    print'Connection made from: ', addr

    while 1:
        data =clientsock.recv(BUFSIZ)
        if not data:
            break
            clientsock.send('echoed', data)

    clientsock.close()
serversock.close()

code for client

from socket import *

HOST ='localhost'
PORT =21567
BUFSIZ =1024
ADDR =(HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while 1:
    data =raw_input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.rec(1024)
    if not data:
        break
print data
tcpCliSock.close()