I have the following program that assigns dictionary keys to an array ( addr[] ) and values to a corresponding array ( msg[] )
import smtplib
class item:
id = 0 # next available Item ID
def __init__(self,startBid,desc):
self.id = item.id
item.id += 1
self.highBid = startBid
self.highBidder = None
self.desc = desc
self.isopen = True
item1 = item(200.30, "bike with a flat tire")
item2 = item(10.4, "toaster that is very large")
item3 = item(40.50, "computer with 8 gb of ram")
clnts = {'test@hotmail.com':[item1,item3], 'test@yahoo.com':[item2] }
def even(num):
if (num % 2 == 0):
return True
else:
return False
def getmsg(clnts):
index = 0
j = 0
msg = []
addr = []
for key in clnts:
addr[j] = key
for key in values:
msg[j] += str(key.highbidder()) + key.highbid()
index += 1
j += 1
getmsg(clnts)
I’ve tried and tried to fix this but I keep getting an error:
line 39, in getmsg
addr[j] = key
IndexError: list assignment index out of range
In Python, you cannot assign to an index that does not already exist:
Instead of
Try
You could do away with
jaltogether, because you’ll have to do the same withmsg.There are some other problems with your code that are not part of your question; I’m assuming these are just errors in trying to make a boiled-down example for a question.