Using a Loop to add objects to a list(python)
Posted
by Will
on Stack Overflow
See other posts from Stack Overflow
or by Will
Published on 2010-04-18T18:30:33Z
Indexed on
2010/04/18
18:33 UTC
Read the original article
Hit count: 212
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 (guess==1):
print("STUDENT")
namer = raw_input("Enter Name: ")
ager = raw_input("Enter Age: ")
sexer = raw_input("Enter Sex: ")
faver = raw_input("Enter Fav: ")
elif(guess==2):
print "TESTING LINE"
elif(guess==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!
© Stack Overflow or respective owner