1

I am working on a library/book program. One function I am coding involves getting input from a user to add a new book to the library. I keep getting this error that seems to involve the situation when no input is entered:

Traceback (most recent call last):

File "python", line 96, in

File "python", line 89, in print_menu

File "python", line 70, in add_book

TypeError: must be str, not NoneType

The steps for this are:

First run the code and enter values for a new title, author or subject. Everything works. The new book is added to the library list. Then, try entering a new book again. When prompted to enter either a new title, author or subject, just press the ENTER key. The code will call the sub function again to ask for input for that item. Enter something the second time around.

I added some code to print things as the code executes. The problem seems to be that the non-entry is being returned by the sub functions. But, I don't know why...which is why I posted here.

library = [["Arctic Dreams", "Barry Lopez", "Nature Writing", 356, 2002], ["A Short History of Nearly Everything", "Bill Bryson", "Science", 405, 2001], ["Hiking the Buckeye Trail", "Greg Hanson", "Outdoors", 210, 2010],["Victory at Sea", "Hans Glutzkin", "History", 250, 1988]]

def print_library():
    for book in library:
        print()
        for idx, content in enumerate(book):
            if idx == 0:
                s = "Title: '"
                e = "'"
            elif idx == 1:
                s = "Author: "
                e = ""
            elif idx == 2:
                s = "Subject: "
                e = ""
            elif idx == 3:
                s = ""
                e = " pages"
            else: # idx = 4
                s = "Year published: "
                e = ""
            print(s + str(content) + e)
        print()


def add_book():
    def add_title():
        new_title = input("Please enter a title:")
        if new_title:
            print("new title:")
            print(new_title) # Addedfor testing
            return new_title          
        else:
            add_title()


    def add_author():
        new_author = (input("Please enter an author:"))
        if new_author:
            print("new author:")
            print(new_author) # Added for testing
            return new_author 
        else:
            add_author()

    def add_subject():
        new_subject = input("Please enter a subject:")
        if new_subject:
            print("new subject:")
            print(new_subject) # Added for testing
            return new_subject 
        else:
            add_subject()

    n_title = add_title()
    n_author = add_author()
    n_subject = add_subject()
    # The next values are temporarily hard-coded until the error is fixed.     
    n_nbr_pages = "435"
    n_year_pub = "2014"

    library.append([n_title, n_author, n_subject, int(n_nbr_pages), int(n_year_pub)])

    print()
    print(library)  # Added for testing .
    print()

    print("\nThe new book:\n")
    print("Title: '" + n_title + "'")
    print("Author: " + n_author)
    print("Subject: " + n_subject)
    print("Number of pages: " + n_nbr_pages)
    print("Year published: " + n_year_pub)


def print_menu():
    print("------------------------------------")
    print("\t    Main Menu:")    
    print("------------------------------------")    
    print("\t1 - List all books")
    print("\t2 - Add a new book")
    print("-------------------------------------\n")
    user_in = input("What would you like to do?")

    if user_in == "1":
        print_library()
    elif user_in == "2":
        print("\nAdd a new book...\n")
        add_book()
    else:
        print("\nInvalid entry. Please try again.")
    print()
    print_menu()


print_menu()

I have come up with a coding solution that doesn't get the error using while loops. But, I still don't understand why my previous code doesn't work. Does it have something to do with the fact that I am initializing a value (new_title = "") to the variables that get the input in the following code that doesn't get the error?

def add_title():
    new_title = ""
    while new_title == "":
        new_title = input("Please enter a title:")
    return new_title

def add_author():
    new_author = ""
    while new_author == "":
        new_author = input("Please enter an author:")
    return new_author

def add_subject():
    new_subject = ""
    while new_subject == "":
        new_subject = input("Please enter a subject:")
    return new_subject
Community
  • 1
  • 1
  • I am returning a value - which seems to be the problem in the other (duplicate) question. So, I don't think my question is a duplicate. – Planet.Megan Jun 05 '18 at 14:45
  • Ha! OK - I see what you mean now. I need to say "return add_title()" instead of just calling the function again. – Planet.Megan Jun 05 '18 at 15:11

0 Answers0