首先,我是 Python 新手,所以请放轻松...其次,我以前从未使用过论坛,所以如果我粘贴了太多代码或没有提供您需要的内容,请原谅我,我会尝试我的最好的:
我需要我的代码做什么:
我需要我的代码向用户询问名为 moduleName 的输入,然后在输入 moduleName 后,我需要它向用户询问该特定模块的等级。输入之后,我需要它再次询问模块然后评分,直到没有更多可以输入的地方,用户将在模块位中输入 -1 以结束它。我还需要将每个项目保存到我创建的全局列表中。因此,当我使用我创建的函数来查看列表时,它会打印出所有模块和成绩。
迄今为止我的代码:(我的全局列表位于名为 students[] 的代码的顶部)
def addStudent():
print
print "Adding student..."
student = Student()
firstName = raw_input("Please enter the student's first name: ")
lastName = raw_input("Please enter the student's last name: ")
degree = raw_input("Please enter the name of the degree the student is studying: ")
studentid = raw_input("Please enter the students ID number: ")
age = raw_input("Please enter the students Age: ")
**moduleName= 0
while moduleName != "-1":
moduleName = raw_input("Please enter module name: ")
grade = raw_input ("Please enter students grade for " + moduleName+": ")
students.append(student)**
student.setFirstName(firstName) # Set this student's first name
student.setLastName(lastName)
student.setDegree(degree)# Set this student's last name
student.setGrade(grade)
student.setModuleName(moduleName)
student.setStudentID(studentid)
student.setAge(age)
print "The student",firstName+' '+lastName,"ID number",studentid,"has been added to the system."
我得到的输出:
我现在已经修复了循环,所以它可以正确中断......我现在唯一的问题是 moduleName 和grade 字段保存到我的全局列表中,但只保存最后一个输入(为-1),而不是输入的多个输入。 .. 如此迷茫。问题也可能出在我创建的这个函数中:
def viewStudent():
print "Printing all students in database : "
for person in students:
print "Printing details for: " + person.getFirstName()+" "+ person.getLastName()
print "Age: " + person.getAge()
print "Student ID: " + person.getStudentID()
print "Degree: " + person.getDegree()
print "Module: " + person.getModuleName()
print "Grades: " + person.getGrade()
再次道歉,我不知道在论坛等上需要什么,所以请放轻松...在此先感谢!=D