Python Forum
I'm blocked in the construction of my program [Novice]
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm blocked in the construction of my program [Novice]
#1
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, zz
And here what I get with >>> iii(abcd):
Error:
Traceback (most recent call last): File "<pyshell#125>", line 1, in <module> iii(a) File "C:/Python34/poiuy.py", line 19, in iii xx += [ppp(e[1][i], e[2][i])] IndexError: tuple index out of range
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!
Reply
#2
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-Nev...n-sequence
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,306 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Total electric construction cost project SecondSandwich94 2 2,148 Jul-21-2021, 09:37 PM
Last Post: deanhystad
  Python Complete novice: Want to run a vulnerability script bhanney23 1 2,976 Sep-18-2020, 09:27 AM
Last Post: Aspire2Inspire
  (Complete Novice) Code not working PythonGainz 9 4,092 Mar-21-2020, 05:53 PM
Last Post: buran
  Novice use of a CSV preliator 1 2,102 Dec-26-2019, 08:05 PM
Last Post: Axel_Erfurt
  Blocked chromebook arsenal58 1 1,989 Feb-05-2019, 02:36 PM
Last Post: perfringo
  Trivial novice question: information gathering tools in Python Drone4four 1 2,251 Nov-10-2018, 01:41 AM
Last Post: Larz60+
  Class construction in python Alberto 3 4,399 Jul-18-2017, 03:37 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020