【发布时间】:2011-02-09 10:35:02
【问题描述】:
我正在尝试使用 while 循环将对象添加到列表中。
这基本上是我想做的:
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))))
我可以将对象添加到列表中,但我不知道如何在循环中将多个对象添加到列表中。
这是我目前的代码:
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")
任何帮助将不胜感激!
【问题讨论】:
-
所有命名为guess的变量都应该命名为choice my wrong
-
您可以编辑您的问题;)
-
哈哈,我试过了,但我得到了 LOLcat 的该死的错误消息页面
标签: python list object loops while-loop