I need some help, please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danne123
    New Member
    • Sep 2006
    • 5

    #1

    I need some help, please

    Hi

    I'm trying remove a person from a list but it's not working.
    How can I make it work, please help me.

    def RemoveName(self ):
    lastname = raw_input("Ente r the last name of the person you want to remove ")
    for i in list_1:
    if lastname == i.lastname:
    list_1.remove(i )
    print "Removed", i.name, i.lastname
    return
    else:
    print "The name doesn't exist."
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I didn't have time to test this, but it shows you some good tricks.
    In your next post, please USE CODE TAGS as described in "posting guidelines", found in the panel on your right when you are posting or in the sticky thread (at the top of this forum). Your post should look like this:

    Code:
    def RemoveName(self):
        lastname = raw_input("Enter the last name of the person you want to remove ")
        # Search a list containing full names
        for i, full_name in enumerate(list_1):
            if lastname in full_name:
                list_1.remove(i)
                print "Removed", i.name, i.lastname
                return
            else:
                print "The name doesn't exist."

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by danne123
      Hi

      I'm trying remove a person from a list but it's not working.
      How can I make it work, please help me.

      Code:
      def RemoveName(self):
          lastname = raw_input("Enter the last name of the person you want to remove ")
          for i in list_1:
              if lastname == i.lastname:
                  list_1.remove(i)
                  print "Removed", i.name, i.lastname
                  return
              else:
                  print "The name doesn't exist."
      Try something like this:
      Code:
      name_list = ["John Smith", "Bob Jones", "Bill Sanders"]
      for i in range(len(name_list)):
          if "Smith" in name_list[i]:
                 del name_list[i]
       	   break
      print name_list
      ['Bob Jones', 'Bill Sanders']
      or this:
      Code:
      for i in range(len(name_list)):
          if name_list[i].split()[1] == "Smith":
                 del name_list[i]
       	   break
      or this:
      Code:
      import string
      name_to_remove = "SMITH"
      for i in range(len(name_list)):
          if string.lower(name_list[i]).endswith(string.lower(name_to_remove)):
              del name_list[i]
              break
      one more:
      Code:
      >>> import string
      >>> name_to_remove = "SMITH"
      >>> name_list = ["John Smith", "Bob Jones", "Bill Sanders", "Frank Smith"]
      >>> for s in name_list:
      ... 	if string.lower(name_to_remove) in string.lower(s):
      ... 		del name_list[name_list.index(s)]
      ... 		
      >>> print name_list
      ['Bob Jones', 'Bill Sanders']

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        But don't import string to use these methods!!! This module has been deprecated (meaning DON'T USE THIS BECAUSE IT MAY NOT ALWAYS BE AVAILABLE). Instead, use the string itself as the object whose method you call:
        Code:
        name = "John Smith"
        print name.lower()

        Originally posted by bvdet
        Try something like this:
        Code:
        name_list = ["John Smith", "Bob Jones", "Bill Sanders"]
        for i in range(len(name_list)):
            if "Smith" in name_list[i]:
                   del name_list[i]
         	   break
        print name_list
        ['Bob Jones', 'Bill Sanders']
        or this:
        Code:
        for i in range(len(name_list)):
            if name_list[i].split()[1] == "Smith":
                   del name_list[i]
         	   break
        or this:
        Code:
        import string
        name_to_remove = "SMITH"
        for i in range(len(name_list)):
            if string.lower(name_list[i]).endswith(string.lower(name_to_remove)):
                del name_list[i]
                break
        one more:
        Code:
        >>> import string
        >>> name_to_remove = "SMITH"
        >>> name_list = ["John Smith", "Bob Jones", "Bill Sanders", "Frank Smith"]
        >>> for s in name_list:
        ... 	if string.lower(name_to_remove) in string.lower(s):
        ... 		del name_list[name_list.index(s)]
        ... 		
        >>> print name_list
        ['Bob Jones', 'Bill Sanders']

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by bartonc
          But don't import string to use these methods!!! This module has been deprecated (meaning DON'T USE THIS BECAUSE IT MAY NOT ALWAYS BE AVAILABLE). Instead, use the string itself as the object whose method you call:
          Code:
          name = "John Smith"
          print name.lower()
          I got the message Barton. My Python Essential Reference is based on 1.5.2, and my applications execute in 2.3, so I may be out of date. I'll try to keep up.

          Comment

          • danne123
            New Member
            • Sep 2006
            • 5

            #6
            hi

            thanks for the answers but what I'm trying do do is that I have a file with the first name, last name, birth day, adress and phonenumber written on seperate lines. Then I have read the information into a list. and then I want user to be able to type in the last name of the person they want to delete and then the person with all the information about birth day, adress and phone number should also be deleted from the list and from the file.

            This is how the whole program looks like.
            How can I make the method Remove work. It only says the name dosen't exist. I have tried to make this work for a long time now. Please help me.

            Code:
             class Person:
                def __init__(self, firstname, lastname, birthday, adress):
                    self.firstname = firstname
                    self.lastname = lastname
                    self.birthday = birthday
                    self.adress = adress
            
            
            
            class Register:
                def readFromFile(self):
                    global name_list
                    name_list = list()
                    name_file = open("names.txt", "r")
                    line = name_file.readline()
                    while line != "":
                        firstname = name_file.readline()
                        lastname = name_file.readline()
                        birthday = name_file.readline()
                        adress = name_file.readline()
                        name_list.append(Person(firstname, lastname, birthday, adress))
                        line = name_file.readline()
                    name_file.close()
            
            
                def Remove(self):
                    name = raw_input("Enter the last name of the person you want to delete ")
                    for i in name_list:
                        if name == i.lastname:
                            name_list.remove(i)
                            print "Removed", i.firstname, i.lastname
                            return
                        else:
                            print "The name doesn't exist."

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              That helps a lot! Here is something to get you started:

              Code:
              class Person:
                  def __init__(self, firstname, lastname, birthday, adress):
                      self.firstname = firstname
                      self.lastname = lastname
                      self.birthday = birthday
                      self.adress = adress
              
              
              
              class Register:
                  def readFromFile(self):
                      global name_list
                      name_list = list()
                      name_file = open("names.txt", "r")
                      line = name_file.readline()
                      while line != "":
                          firstname = name_file.readline()
                          lastname = name_file.readline()
                          birthday = name_file.readline()
                          adress = name_file.readline()
                          name_list.append(Person(firstname, lastname, birthday, adress))
                          line = name_file.readline()
                      name_file.close()
              
              
                  def Remove(self):
                      name = raw_input("Enter the last name of the person you want to delete ")
                      for person in name_list:
                          if name == person.lastname:
                              name_list.remove(person)
                              print "Removed", person.firstname, person.lastname
                              return
                      else:
                          print "The name doesn't exist."
              
              
              if __name__ == "__main__":  # standard way to test a module
                  name_list = []
                  p = Person('joe', 'blow', 'june 12 1972', '1234 anywhere')
                  name_list.append(p)
                  p = Person('john', 'doe', 'june 12 1972', '1234 anywhere')
                  name_list.append(p)
              
                  print name_list
              
                  reg = Register()
                  reg.Remove()
              
                  print name_list
              Originally posted by danne123
              hi

              thanks for the answers but what I'm trying do do is that I have a file with the first name, last name, birth day, adress and phonenumber written on seperate lines. Then I have read the information into a list. and then I want user to be able to type in the last name of the person they want to delete and then the person with all the information about birth day, adress and phone number should also be deleted from the list and from the file.

              This is how the whole program looks like.
              How can I make the method Remove work. It only says the name dosen't exist. I have tried to make this work for a long time now. Please help me.

              Code:
               class Person:
                  def __init__(self, firstname, lastname, birthday, adress):
                      self.firstname = firstname
                      self.lastname = lastname
                      self.birthday = birthday
                      self.adress = adress
              
              
              
              class Register:
                  def readFromFile(self):
                      global name_list
                      name_list = list()
                      name_file = open("names.txt", "r")
                      line = name_file.readline()
                      while line != "":
                          firstname = name_file.readline()
                          lastname = name_file.readline()
                          birthday = name_file.readline()
                          adress = name_file.readline()
                          name_list.append(Person(firstname, lastname, birthday, adress))
                          line = name_file.readline()
                      name_file.close()
              
              
                  def Remove(self):
                      name = raw_input("Enter the last name of the person you want to delete ")
                      for i in name_list:
                          if name == i.lastname:
                              name_list.remove(i)
                              print "Removed", i.firstname, i.lastname
                              return
                          else:
                              print "The name doesn't exist."

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by bvdet
                I got the message Barton. My Python Essential Reference is based on 1.5.2, and my applications execute in 2.3, so I may be out of date. I'll try to keep up.
                Sorry, BV. I missed this post and have been aiming my posts at getting your attention.

                Comment

                Working...