Hey all,

So basically I need to change the code so that the phonebook and the inverse phonebook are easily updated and maintained. Also, whenever I add an entry, both phonebooks need to be updated.

Lastly, when I write a phonebook to a file, write a backwards phonebook as well.
Do your best to handle cases when two individuals share a phone number. If both Fido and Lucky have the same phone number (212-888-3434). One of them will not be in the backwards phonebook unless, the backwards phonebook is modified.
Suggestion: for the backwards phonebook, allow the values to be a list of names, rather than a single name. Alternatively, use a string that includes all of the barers of a phone number. Thus, in the above example, the value for '212-888-3434' could be 'Fido and Lucky' or .

Help would be GREATLY appreciated!

import os
telephone_book = {}
inverse_phonebook = {}

def add_number_to_phone_book(key,value):
    telephone_book[key] = value

def add_line_to_phone_book(line):
    words = line.split()
    name = ''
    if len(words) >= 2:
        for word in words[:-1]:
            name = name+' '+word
        name = name[1:]
        add_number_to_phone_book(name,words[-1])
    else:
        print('Warning null entry')   

def add_phone_numbers_from_file(file):
    new_numbers = open(file, 'r')
    for line in new_numbers:
        add_line_to_phone_book(line)
    new_numbers.close()

def write_phone_numbers_to_file(file):
    write = open(file,'w')
    for key in telephone_book:
        write.write(key+' '+ telephone_book[key]+os.linesep)
    write.close()
    
def create_inverse_phone_book(dictionary):
    inverse_phonebook.clear()
    for key in dictionary:
        inverse_phonebook[dictionary[key]] = key

def create_inverse_phone_book2(dictionary):
    inverse_phonebook.clear()
    for key in dictionary:
        if telephone_book[key] in inverse_phonebook:
            inverse_phonebook[dictionary[key]] = \
                inverse_phonebook[dictionary[key]]\
                + ' and ' + key
        else:
            inverse_phonebook[dictionary[key]] = key

def create_inverse_phone_book3(dictionary):
    ## This is an alternative using lists as values
    inverse_phonebook.clear()
    for key in dictionary:
        if telephone_book[key] in inverse_phonebook:
            inverse_phonebook[dictionary[key]].append(key)
        else:
            inverse_phonebook[dictionary[key]] = [key]

Recommended Answers

All 4 Replies

And the problem and error messages?

I don't know how to implement the code, basically I don't know where to start.

There are no error messages when I run it just like the code above, but that is probably because I haven't ended the function yet.

Usually best place to start is beginning and one step at time keeps you from falling better that ten steps at time :)

Where the phone numbers would be in the beginning, how you can access them by given functions? etc.

Make a story ('pseudocode') what happens when user uses program and start coding one function at time.

BTW my way would be yet another way to update phonebooks syncronized:
I would add another line of code to add_number_to_phone_book and not use those create_inverse... stuff


And why would not one user have more than one phone number? So the relationship would be many-to-many.

i dont think you wrote this code yourself. Any way what is the problem with your application?

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.