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!
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)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With