Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hey guys so im trying to use a while loop to add objects to a list.

Heres bascially what i want to do: (ill paste actually go after)

class x:
     blah
     blah

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))))

as im doing this i can get the object to get added to the list, but i am stuck as to how to add multiple objects to the list in the loop.

Here is my actual 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!

share|improve this question
all variables named guess should be named choice my mistake – Will Apr 18 '10 at 18:34
You can edit your question ;) – Felix Kling Apr 18 '10 at 18:38
haha i tried to but i got the damn error message page with the LOLcat – Will Apr 18 '10 at 18:39
Did it for you... – Felix Kling Apr 18 '10 at 20:52

1 Answer

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)
share|improve this answer
so outside the loop i should have a=[] and then inside the loop i should have a.append(s)? – Will Apr 18 '10 at 18:37
@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? – Mark Byers Apr 18 '10 at 18:40
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 – Will Apr 18 '10 at 18:42
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 Apr 18 '10 at 18:47
@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. – Mark Byers Apr 18 '10 at 18:48
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.