#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2

    Need help with deleting and adding input


    Hi, the following program is supposed to allow the user to store,del or view the information he stored. I got 2 issues. At def addStudent() , for the module and mark input; I am trying to make it a loop so that the user can enter multiple module and marks under the same name. But the modules are remaining as single input. Also, I am using the word 'done' to stop the loop. This word 'done' ends up as the module name.. Somehow this problem seems so familiar and yet been cracking head for sometime now and nothing working..

    Secondly for def removeStudent(), I am a little confused about the deleting process. the way I see it, right now it is a class object which holds a list which holds list objects plus a dictionary. I don't know if I should work to delete the class object, or delete by indicating del commands for list items and dictionary separately. (Hope I made sense here..)

    Have attached the codes below. Tnks for guidance.

    Code:
    students = [] 
    class Student: 
        firstName = "" 
        lastName = "" 
        age = 0 
        studentID = "" 
        degree = "" 
        grades = {} 
    
    def setFirstName(self, firstName): 
        self.firstName = firstName 
    
    def getFirstName(self): 
        return self.firstName 
    
    def setLastName(self, lastName): 
        self.lastName = lastName 
    
    def getLastName(self): 
        return self.lastName 
    
    def setDegree(self, degree): 
        self.degree = degree 
    
    def getDegree(self): 
        return self.degree 
    
    def setGrades(self, grades): 
        self.grades = grades 
    
    def getGrades(self): 
        return self.grades 
    
    def setStudentID(self, studentID): 
        self.studentID = studentID 
    
    def getStudentID(self): 
        return self.studentID 
    
    def setAge(self, age): 
        self.age = age 
    
    def getAge(self): 
        return self.age 
    
    def addStudent(): 
        print "Adding student..."
        F1 = raw_input("Please enter the student's first name: ")
      
          F2 = raw_input("Please enter the student's last name: ") 
          F3 = raw_input("Please enter the student's degree: ") 
          F4 = raw_input("Please enter the student's ID: ") 
          F5 = raw_input("Please enter the student's age: ") 
          F6 = '0' 
          F7 = '-2' # 'done' added as module name, only able to print single module.. 
    
        while F6 != 'done': 
            F6 = raw_input("Please enter module name. type 'done' to quit: ") 
            if F6 == 'done': break 
                F7 = raw_input("Please enter the grades for " ':') 
    
            student = Student() student.setFirstName(F1)   
            student.setLastName(F2) student.setDegree(F3)   
            student.setStudentID(F4) student.setAge(F5) 
            student.setGrades({F6:F7}) 
            students.append(student) 
            print "Student added" 
    
    def printStudents(): 
        print "Printing all students in the database... " 
        for person in students: 
            print "Printing details for " + person.getFirstName() + (person.getLastName()) 
            print "Degree: " + person.getDegree() 
            print "Student ID: " + person.getStudentID() 
            print "Age: " + person.getAge() 
            print "Grades: " + str(person.getGrades()) 
            print "" 
    
    def removeStudent(): #Wrong
        print 'Removing a student from the database...'
        userInput2 = raw_input('Please enter Student ID of student to be deleted: ') 
        if userInput2 == studentID: 
            del studentID 
        print 'removed from the database.' 
        else: 
            print 'No matching student found.' 
    
    print "-------------------------------------" 
    print "Welcome to the student record system!" 
    print "-------------------------------------" 
    print "" 
    
    userInput = 0 
    while userInput != '-1': 
        print '---------------------------------------' 
        print 'Menu' 
        print '1. Add a student (to the database)' 
        print '2. Print all students (in the database)' 
        print '3. Remove a student (from the database)' 
        print '---------------------------------------' 
        userInput = raw_input('What would you like to do <type a number or -1 to quit. ') 
        if userInput == '1': 
            addStudent() 
        elif userInput == '2': 
            printStudents() 
        elif userInput == '3': 
            removeStudent() 
        elif userInput == '-1': 
            print 'End of program' 
        else: 
            print 'Sorry', userInput ,'is not a valid option. Try again.'
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    After correcting the indentation errors in your post, after inserting semi-colons into juxtaposed lines, changing your student deletion code a bit the program becomes functional.

    Deletion from a list takes place by slice or item number. You need to search the list for an object matching the student ID, then use the index number to remove it from the list.
    Code:
    def removeStudent():
        print 'Removing a student from the database...'
        userInput2 = raw_input('Please enter Student ID of student to be deleted: ')
        for (i,student,) in enumerate(students):
            if userInput2 == student.getStudentID():
                del students[i]
                print 'removed from the database.'
                break
        else:
            print 'No matching student found.'
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Thank you, it works but can I check something regarding the 3rd line below.

    Code:
     for (i,student,) in enumerate(students):
    
            if userInput2 == student.getStudentID():
                del students[i]
    1.The first line says, for i in student(this is an object mirroring same properties as the previously created class), iterate through the list called students to locate i.

    2. If the userinput2 matches the studentID

    3. For the 3rd line, my understanding is if 'i' matches, delete 'i' in the list called student. But doesnt that means it should only delete i which is the student id only and not the entire information.

    Example:

    a list contains
    A = Apple
    B = Banana
    C = Carrot

    The above del removes all 3 information if 'i' = Apple. But from my understanding only Apple gets removed while Banana and Carrot stays. Think getting twisted and another long post. Sorry, hope to get some ideas. Thank you.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    The first line

    for (i,student,) in enumerate(students):

    says "iterate through the iterable students. One by one assign both an index and the next object to the respective variables i and student, then execute the block of following statements."

    For example, and you could do this experiment yourself, and it looks better in python3,
    Code:
    >>> for (i,o,) in enumerate('Dave'):
    ...     print('i =',i)
    ...     print('and str(o) is',str(o))
    ...     print('--------------------------Going to the next iteration if there is one')
    ... 
    ('i =', 0)
    ('and str(o) is', 'D')
    --------------------------Going to the next iteration if there is one
    ('i =', 1)
    ('and str(o) is', 'a')
    --------------------------Going to the next iteration if there is one
    ('i =', 2)
    ('and str(o) is', 'v')
    --------------------------Going to the next iteration if there is one
    ('i =', 3)
    ('and str(o) is', 'e')
    --------------------------Going to the next iteration if there is one
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    2
    Ah, so 'i' represents like an invisible count of the no. of iteration while 'o' or in my case 'student' represents the object to be printed out of the list. It can be done on both a list containing only str, dict or a combination of both. Tnks for input. Just for sharing, I tried out the following to clarify the concept. Thanks for help.

    Code:
    students = [10,20,30,40,50] # Trying a list containing only str 
    
    for (i,student,) in enumerate(students): 
        print student 
    
    students1 = [{'a':'1', 'b':'2'} ,{'c':'3', 'd':'4'}] #trying a list containing only Dict 
    
    for (i,student,) in enumerate(students1): 
        print student 
    
    students2 = [10,20,30,{'a':'1','b':'2'}] # trying a list containing both Dict and str 
    
    for (i,student,) in enumerate(students2): 
        print student

IMN logo majestic logo threadwatch logo seochat tools logo