The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
IndexError:
Discuss IndexError: in the Python Programming forum on Dev Shed. IndexError: Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 17th, 2003, 03:30 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
IndexError:
Ok i created an irc bot using the socket module, but.. its bugged.. when i am in irc.. and i kill the bot, in my konsole i get this error
Traceback (most recent call last):
File "./4.py", line 39, in ?
print Recv(s,1025)
File "./4.py", line 16, in Recv
if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot":
IndexError: list index out of range
Can anyone tell me what this means?
all so when the bot enters the chan i made it say hello, but when i view my konsole i dont see anything about the bot saing hello all i see is what the users in the irc inputed.. here is the code
Code:
#!/usr/bin/env python
from socket import *
from string import *
def Send(self,data):
self.send(data)
def Recv(self,integras):
data = self.recv(integras)
receive = split(data," ")
if receive[0] == "PING": Send(self,"PONG :"+receive[0]+"\n\r")
if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot":
user = split(receive[0],"!")
Send(self,"PRIVMSG #bot :Dont bother me im just a stupid bot\n\r")
return "PRV From"+user[0]+"\\"+receive[3]
#if receive[3] == "!quit": Send(s,"QUIT\n\r")
else:
return data
s = socket(AF_INET,SOCK_STREAM) #Initializing TCP
msg = 0
try:
if s.connect(("irc.server.net",6667)) != -1:
Send(s,"NICK Pythonbot\n\r")
Send(s,"USER localhost localhost localhost : ****YOU\n\r")
Send(s,"JOIN #bot\n\r")
msg = "hello"
Send(s,"PRIVMSG #bot :"+msg+"\n\r")
while 1:
print Recv(s,1025)
msg =+ 1
except error, detail:
print detail[0]
sorry still new to python, and still learning 
any suggestions, Since python are not to big on tutorials
Last edited by xlordt : November 17th, 2003 at 03:33 AM.
|

November 17th, 2003, 09:02 AM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
I guess this could be your mistake :
Code:
Send(s,"NICK Pythonbot\n\r")
You're sending a string to your script with one whitespace.
Code:
receive = split(data," ")
This creates a tuple with 2 elements. So here comes the problem:
Code:
if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot":
I'd say that's the problem. You're trying to access the third element of your 2-tuple ! Remember, Python's lists and tuples work like C arrays : the nth element is accessed by number n-1. So you only need to change it that way :
Code:
if receive[0] == "PRIVMSG" and receive[1] == "Pythonbot":
This should do the trick.
|

November 17th, 2003, 09:32 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Wow, very surprised! i figured that an IRC bot would be allot bigger than this, my bad
Anyway looks like you beat me too it solar  .. anyway i think your right, obviously your trying to access values which dont exist X  , too be safe you could check its size before you carry on although you dont 'need' too..
Code:
if len(recieve) > 1:
...
carry on
...
Next  , you really don't need the string module anymore (at least in my oppinion), you can do everything the string module does more efficently using string methods i.e.
Code:
receive = data.split()
If you insist on using the string module though you should know that split() defaults to spaces so you should write..
Code:
receive = split(data)
..instead of..
Code:
receive = split(data," ")
Quote: Solar
This creates a tuple with 2 elements. So here comes the problem: |
Its actually a list not a tuple. it may not seem like a big deal but there are allot of things you just can't do with tuples so i figured i'd just point this one out
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : November 17th, 2003 at 09:35 AM.
|

November 17th, 2003, 10:27 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Quote:
def Send(self, data):
...
...
def Recv(self, integras):
...
... |
I just knowticed the 'self' option in your function definitions.. really not the best idea X  its would probably help to think of 'self' as a reserved word (although it isnt actually) for use inside classes.. possibly use 'this' or 'object' etc.
Mark.
|

November 17th, 2003, 04:16 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
Quote: This creates a tuple with 2 elements. So here comes the problem:
code:if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot": |
are you sure.. cause.. see what im doing is im splitting the data that i get before it enters the irc chan, there for when i split it then receive[1] will be == to PRIVMSG, now receive[0] will be the users host... im confused now 
|

November 17th, 2003, 07:07 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Ok for everyone else not part of the Netytan -> X meeting, we've solved the problem and X came up with an even smaller piece of code than the one he had before (this one works)
Anyway i created a class to handle this (attached), feel free to download and learn..
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|