14

I'm trying to use a while loop to add objects to a list.

Here's basically what I want to do:

class x:
     pass

choice = raw_input(pick what you want to do)

while(choice!=0):
    if(choice==1):
       Enter in info for the class:
       append object to list (A)
    if(choice==2):
       print out length of list(A)
    if(choice==0):
       break
    ((((other options))))

I can get the object added to the list, but I am stuck at how to add multiple objects to the list in the loop.

Here is the code I have so far:

print "Welcome to the Student Management Program"

class Student:  
    def __init__ (self, name, age, gender, favclass):  
         self.name   = name  
         self.age    = age  
         self.gender = gender  
         self.fac = favclass  

choice = int(raw_input("Make a Choice: " ))

while (choice !=0):
    if (choice==1):  
        print("STUDENT")  
        namer = raw_input("Enter Name: ")  
        ager = raw_input("Enter Age: ")  
        sexer = raw_input("Enter Sex: ")  
        faver = raw_input("Enter Fav: ")      

    elif(choice==2):
        print "TESTING LINE"
    elif(choice==3):
        print(len(a))

    guess=int(raw_input("Make a Choice: "))

    s = Student(namer, ager, sexer, faver)
    a =[];
    a.append(s)

raw_input("Press enter to exit")

Any help would be greatly appreciated!

3
  • all variables named guess should be named choice my mistake Commented Apr 18, 2010 at 18:34
  • You can edit your question ;) Commented Apr 18, 2010 at 18:38
  • haha i tried to but i got the damn error message page with the LOLcat Commented Apr 18, 2010 at 18:39

2 Answers 2

32

The problem appears to be that you are reinitializing the list to an empty list in each iteration:

while choice != 0:
    ...
    a = []
    a.append(s)

Try moving the initialization above the loop so that it is executed only once.

a = []
while choice != 0:
    ...
    a.append(s)
Sign up to request clarification or add additional context in comments.

8 Comments

so outside the loop i should have a=[] and then inside the loop i should have a.append(s)?
@Will: That is probably a good start, although there are some other issues with your code. If you enter a number other than 0 or 1 you will add the same student to the list again. Is this really what you want?
haha and um no....i want the ability to go through the loop and add a different student each time i choose 1... so i would go through once, add a student, choose 1 again and add a different student...etc...etc so each time i go through i can add a diff student
but at the end of the loop iw ould have the user enter in new information for the student class so wouldnt that take the place of the old info?
@Will: I would move the code that appends to the list to be inside the if choice == 1: block. It should not run in the other cases.
|
0

Auto-incrementing the index in a loop:

myArr[(len(myArr)+1)]={"key":"val"}

1 Comment

not working ! IndexError: list assignment index out of range

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.