Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2012, 04:12 PM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 37 m 26 sec
Reputation Power: 1
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.'

Reply With Quote
  #2  
Old December 5th, 2012, 09:08 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,387 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 10 m 30 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old December 6th, 2012, 10:43 AM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 37 m 26 sec
Reputation Power: 1
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.

Reply With Quote
  #4  
Old December 6th, 2012, 11:47 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,387 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 10 m 30 sec
Reputation Power: 383
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
>>> 

Reply With Quote
  #5  
Old December 6th, 2012, 04:33 PM
keshk keshk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 36 keshk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 37 m 26 sec
Reputation Power: 1
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help with deleting and adding input

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap