Hey guys,
I'm trying to create a buy and sell program where I have two lists of numbers. Eventually I want to shorten the lists so that the program buys at a lower number, and sells at a higher number, ignoring all other numbers in between.
The solution would be
BuyList = [1,9,11]
SellList = [7,10,12]
I'm having problems with the range in my for loops.
BuyList = [1,2,3,4,9,11]
SellList = [0,7,8,10,12,15]
length_of_buy_list = []
length_of_sell_list = []
myrange = 0
length_of_buy_list = len(BuyList)
length_of_sell_list = len(SellList)
if length_of_buy_list < length_of_sell_list:
myrange = length_of_buy_list
elif length_of_buy_list > length_of_sell_list:
myrange = length_of_sell_list
else:
myrange = length_of_buy_list #if they are the same length, just pick one.
for x in range(0,myrange-1): #end of range is smaller list so it doesnt go out of range. keep deleting items until everything is in the same range.
if SellList[x] > BuyList[x]:
del BuyList[x]
if length_of_buy_list < length_of_sell_list:
myrange = length_of_buy_list
elif length_of_buy_list > length_of_sell_list:
myrange = length_of_sell_list
else:
myrange = length_of_buy_list #if they are the same length, just pick one.
for x in range(0,myrange-1): #end of range is smaller list so it doesnt go out of range. keep deleting items until everything is in the same range.
if (SellList[x] - BuyList[x]) < 0:
del SellList[x] #This takes the zero out of my SellList. Delete at the end of the loop otherwise stuff before it will mess up range.
if length_of_buy_list < length_of_sell_list:
myrange = length_of_buy_list
elif length_of_buy_list > length_of_sell_list:
myrange = length_of_sell_list
else:
myrange = length_of_buy_list #if they are the same length, just pick one.
print SellList
print BuyList
Here is a common error I get:
Traceback (most recent call last):
File "C:\Python26\06_June_2009\BuyAndSell\BuyAndSellTest2.py", line 19, in <module>
if SellList[x] > BuyList[x]:
IndexError: list index out of range
I tried to address it by constantly changing the myrange integer to always be appropriate for the length of the smallest list. Any other ideas?