I'm blocked in the construction of my program [Novice] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I'm blocked in the construction of my program [Novice] (/thread-10456.html) |
I'm blocked in the construction of my program [Novice] - abcd - May-22-2018 Hello, here's my program: oooooo =['fg', 'dh', 'df', 'hg', 'sd', 'hnb', 'eee', 'rh', 'ddd', 'vvvv'] aaaaaaa = [542, 861,532,7854,2213,45612,7865,1535,453,452] bbbbbb = [8888,7777,4444,1111,2222,333,555,444,123,453] ccccc = [8888,666,555,4444,222,555,666,444,111,222] fffff = [444,555,555,555,666,8888,4444,8888,4444,77777] abcd = [oooooo]+[aaaaaaa] +[bbbbbb]+[ccccc]+ [fffff] def ppp(x, y): return ((y - x)/x)* 100 def iii(*e): xx = [] yy= [] zz = [] for i in range(len(e[0])): xx += [ppp(e[1][i], e[2][i])] for j in range(len(e[0])): yy += [ppp(e[2][j], e[3][j])] for l in range(len(e[0])): zz += [ppp(e[3][l], e[4][l])] return xx, yy, zzAnd here what I get with >>> iii(abcd): I don't understand why my list is called a tuple because there are no () but [] and I don't understand why the index is out of range while it shouldn't because 0 =< i < 10, and the length of the lists = 10. I would like someone to help me please, thank you! RE: I'm blocked in the construction of my program [Novice] - buran - May-22-2018 First of all - get rid of habit to use cryptic variable names as soon as possible... Your variable names are plain nightmare.... As to your question - look at line 14: def iii(*e): By using star in front of e, you make e a catch-all argumenmt, i.e. it turns to be a tuple that holds what you pass as arguments. In your case you pass a single list of lists, so it becomes a single element tuple. That is why e[1] on line 19 raise that exception. Add print(type(e)) print(e)to see for yourself. Finally, read https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence |