-2

Due to my recent interest in Information Security and network programming, I've decided to learn Python through the Internet. I'm trying to write a piece of code which would allow the user to store the Biodata(name, age and job) of the desired number of people. And then the O/P would consist of the biodata of all of those people along with the people count. Being new to Python, I'm not able to identify the error. Thanks - Sid

Here's my 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):
            for myBio in range(0, Bio.counts):
                    print myBio
    while 1:
        name = raw_input("Enter your name: ")
        age = int(raw_input("Enter your age: "))
        job  = raw_input("Enter your 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 the trace of my O/P:

   ./bio.py
   Enter your name: Sid
   Enter your age: 21
   Enter your 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*
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
Nitaai
  • 2,577
  • 2
  • 15
  • 19
  • 3
    Your code has a number of problems. You should read [the Python tutorial](http://docs.python.org/tutorial/) to familiarize yourself with the basics of Python. – BrenBarn May 21 '13 at 05:02
  • 1
    There isn't really a good reason to use a list for this. Plain attributes, or a `dict`, if you have some desperate need for another data structure, would be more appropriate. Also note that `counts` and `myBio` are *class variables*, not *instance variables*, which probably isn't what you want. It means they are the same for all instances. Make them in `__init__()` instead. – Gareth Latty May 21 '13 at 05:07
  • You know, when you type in a title when asking a question, you get a list of similar questions. In this case the list of questions with your exact error is *long* and reading *any* of those questions would have answered your question, and been quicker too. Try that next time. – Lennart Regebro May 21 '13 at 05:07
  • 1
    @LennartRegebro you forgot to mention they are also listed on the right under 'Related' :) – Burhan Khalid May 21 '13 at 05:45
  • @BurhanKhalid: Sure, but that's *after* the question was posted. The list above is *before* you post it. – Lennart Regebro May 21 '13 at 07:10
  • Alright I apologize. And thanks. – Nitaai May 21 '13 at 08:46

2 Answers2

1

This is what you want:

class Bio:

    def __init__(self, name, age, job):
        self.name = name
        self.age = age
        self.job = job
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
-1

Probably what you are looking for is:

def __init__(self, name, age, job):
    Bio.myBio.append(name)
    Bio.myBio.append(age)
    Bio.myBio.append(job)
    Bio.counts + 1

The problem is that the elements Bio.counts, Bio.counts+1, and Bio.counts+2 don't exist when you create the class; you have to create them with the append method.

NOTE

You probably don't need the Bio.counts variable. You can rewrite display as

def display(self):
    for myBio in Bio.myBio:
        print myBio

You also probably want to think about why you are using Bioinstead of self.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86