python - IndexError: list assignment index out of range (in __init__) -
this question has answer here:
due recent interest in information security , network programming, i've decided learn python through internet. i'm trying write piece of code allow user store biodata(name, age , job) of desired number of people. , o/p consist of biodata of of people along people count. being new python, i'm not able identify error. - sid
here's code:
#!/usr/bin/python class bio: counts = 0 mybio = [] def __init__(self, name, age, job): bio.mybio[bio.counts] = name bio.mybio[bio.counts+1] = age bio.mybio[bio.counts+2] = job bio.counts + 1 def display(self): mybio in range(0, bio.counts): print mybio while 1: name = raw_input("enter name: ") age = int(raw_input("enter age: ")) job = raw_input("enter job: ") details = bio(name, age, job) details.display() print "detail count %d" % bio.mybio anymore = raw_input("anymore details ?: (y/n)") if anymore == 'n': break
here's trace of o/p:
./bio.py enter name: sid enter age: 21 enter job: infosec traceback (most recent call last): file "./bio.py", line 25, in <module> details = bio(name, age, job) file "./bio.py", line 9, in __init__ bio.mybio[bio.counts] = name indexerror: list assignment index out of range*
this want:
class bio: def __init__(self, name, age, job): self.name = name self.age = age self.job = job
Comments
Post a Comment