2

So I'm not sure if the title even makes sense but basically I have a class that keeps track of friends (like an address book), and I'm at the part where I need to create a function that adds friends to a set of existing names. So I've got the beginning of my code as:

class SocialAddressBook:
    def __init__(self):
        self.book= []


    def addName(self, name, address):
        self.book.append([name, address, set()])
        """Adds name to address book, with address and no friends"""

which runs fine. and then the part that gives me 'list' object has no attribute 'list' error:

def addFriend(self, name, friend):
    for k in range(len(self.book.list[k])):
        if self.book[k][0] == name:

my idea was to iterate over a list and add onto the existing list, but syntax is definitely throwing me for a loop (no pun intended) and I'm not sure how I should go about this now.

some test code:

a.addFriend('Fred', 'Barb'); a.addFriend('Fred', 'Sue')
a.addFriend('Barb', 'Jane'); a.addFriend('Jane', 'Emma')
a.addFriend('Jane', 'Mary'); a.addFriend('Emma', 'Lisa')

Thank you!

2
  • Why are you doing self.book.list, I think think lists have a list property. Commented Mar 28, 2017 at 15:35
  • 1
    Usually you don't need an index to iterate over collections in Python. Simply for book_name, address, friends in self.book: and then if book_name == name: would do, depending on what comes after. Probably friends.add(friend)? Commented Mar 28, 2017 at 15:37

2 Answers 2

4

Your for loop in your addFriend method is incorrect. Specifically, self.book.list[k].

First off, self.book is a list, and lists do not have a list property. Second, you can't use k there as it does not yet exist.

If you want to loop over a list, why not just do that? You don't need to use range().

for book in self.book:
    if book[0] == name:
        pass
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! For more advanced coding, you can actually unpack the values in the for loop. for b_name, b_address, b_set in self.book: then instead of using book[0], you can use b_name.
2

If you can, your solution should be implemented using a dictionary instead of a list to improve performance of your address book. Adding names is trivial, and other than the error checking involved, adding friends is also trivial. For a more complete solution, a remove_name method has been added showing how to correctly remove someone.

import collections


def main():
    book = SocialAddressBook()
    # Add people to the address book.
    book.add_name('Fred', '[email protected]')
    book.add_name('Barb', '[email protected]')
    book.add_name('Jane', '[email protected]')
    book.add_name('Emma', '[email protected]')
    book.add_name('Sue', '[email protected]')
    book.add_name('Mary', '[email protected]')
    book.add_name('Lisa', '[email protected]')
    # Add friends connections in the address book.
    book.add_friend('Fred', 'Barb')
    book.add_friend('Fred', 'Sue')
    book.add_friend('Barb', 'Jane')
    book.add_friend('Jane', 'Emma')
    book.add_friend('Jane', 'Mary')
    book.add_friend('Emma', 'Lisa')


class SocialAddressBook:

    def __init__(self):
        self.__book = {}

    def add_name(self, name, address):
        """Adds name to address book with address and no friends."""
        self.__book[name] = BookEntry(address, set())

    def add_friend(self, name, friend):
        """Adds a friend to the person referenced by name."""
        if friend not in self.__book:
            raise ValueError(f'{friend!r} is not in the address book yet')
        self.__book[name].friends.add(friend)

    def remove_name(self, name):
        """Completely delete someone from the address book."""
        del self.__book[name]
        for book_entry in self.__book.values():
            book_entry.friends.discard(name)


BookEntry = collections.namedtuple('BookEntry', 'address, friends')


if __name__ == '__main__':
    main()

Please read PEP 8 -- Style Guide for Python Code soon if possible. Code written in Python should conform to the style guide to promote maintainability and to encourage others to read your code and answer your questions. In the future, please create a Minimal, Complete, and Verifiable example of your problem before asking your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.