import sys
import easygui

def choose_book(books, title):
    '''Return the name of a book (a string) from among the keys in books.
    title (a string) is a title for the operation the user is performing.'''
    
    msg = 'Choose a book'
    options = books.keys()
    choice = easygui.choicebox(msg, title, options)
    easygui.msgbox(choice, 'Book you chose')
    return choice
    
def choose_borrower(books, title):
    '''Return the name of a borrower (a string) from among the keys in books.
    title (a string) is a title for the operation the user is performing.'''
    
    msg = 'Choose a borrower'
    options = books.values()
    choice = easygui.choicebox(msg, title, options)
    easygui.msgbox(choice, 'Borrower you chose')
    return choice
    
def check_out(books):
    book = choose_book(books, "Check out")
    if books[book] == "home":
        borrower = choose_borrower(books, "Check out")
        easygui.msgbox("Lending %s to %s." % (book, borrower))
        books[book] = borrower
    else:
        easygui.msgbox("I can't lend it!  %s has it." % (books[book]))
    
def hand_in(books):
    book = choose_book(books, "Hand in")
    if books[book] == "home":
        easygui.msgbox("It's already at home!")
    else:
        borrower = choose_borrower(books, "Hand in")
        easygui.msgbox("Getting %s back from %s." % (book, borrower))
        if books[book] != borrower:
            easygui.msgbox("I don't know how %s got it, but I'll take it back." 
                           % (borrower))
        books[book] = "home"
    
def list(books):
    easygui.msgbox(books.items())

if __name__ == "__main__":
    
    books = {"Great Expectations": "home", "Harry Potter": "William", \
             "A Thousand Splendid Suns": "Tom", "Oryx and Crake": "home", \
             "The Hobbit": "Sven"}

    prompt = 'What would you like to do?'
    title = 'Main choices'
    stop = False
    
    while not stop:
        
        choice = easygui.buttonbox(prompt, title, ("out", "in", "list", "quit"))
        if choice:
            if choice == "out":
                check_out(books)
            elif choice == "in":
                hand_in(books)
            elif choice == "list":
                list(books)
            elif choice == "quit":
                stop = True
             