Firstly, I am new to Python so please go easy on me… Secondly, I’ve never used a forum before so if I paste too much code or don’t give exactly what you need, forgive me, I shall try my best:
WHAT I NEED MY CODE TO DO:
I need my code to ask the user for an input called moduleName then after moduleName is entered I need it to ask the user for the grade for that specific module. After that is entered I need it to ask again for module then the grade until there are no more to enter where by the user will enter -1 into the module bit to end it. I also need each item to save to a global list I have created. So when I use teh function I have created to view the list it prints out all modules and grades.
MY CODE THUS FAR: (my global list is at top of code named 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."
THE OUTPUT I GET:
I have now fixed the loop so it breaks correctly… the only problem I have now is that the moduleName and grade fields save to my global list but only save the last input (being -1) as opposed to the multiple inputs entered… so confused.
The problem may also lie within this function I have created:
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()
Again apologies I don’t know what’s needed on forums etc so go easy on me…
Thank in Advance! =D
I would suggest replacing the
while moduleName != "-1":loop with awhile True:loop, and then insert this code after asking for the module name:This will break out of the while loop when you want it to.
Addressing your second question, the
appendfunction is in theelsebit of the wholewhileloop. This means that it only works when the function breaks. You need to put this in the mainwhileloop after you get the input, and get rid of theelse.Also, I don’t see
studentdefined anywhere – what is it meant to be?Here’s the code you want: