Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

AttributeError: 'NoneType' object has no attribute 'format'

When I use this with python3 I get the following error:

"AttributeError: 'NoneType' object has no attribute 'format'"

in my add_item function, in reference to the line

"print("Added {} to shopping list. Shopping list now has {} items.".format(item, len(shopping_list)))".

However the error disappears and everything works when I remove

"shopping_list.append(item)"

from the add_item function.

Here's my code:

import os


# make a list that will hold our items
shopping_list = []

print("What should we pick up at the store?")

def clear_screen():
    os.system("cls" if os.name == "nt" else "clear")

def show_instructions():
    clear_screen()
    # print out instructions on how to use it
    print("")
    print("Enter done to stop adding stuff")
    print("Enter show to see your shopping list")
    print("Remove to delete an item from your list")
    print("Enter help to see these instructions again")

def show_list():
    # print out the list
    clear_screen()
    print("Here's your list")
    index = 1
    for item in shopping_list:
        print("{}. " + item).format(index)
        index+=1

    print("-" * 10)
    print("")

def add_item(item):
    shopping_list.append(item)
    print("Added {} to shopping list. Shopping list now has {} items.".format(item, len(shopping_list)))
    show_list()

def remove_item():
    show_list()
    print("What would you like to remove?")
    rem_item = input("> ")
    try:
        shopping_list.remove(rem_item)
    except ValueError:
        pass
    show_list()

show_instructions()

# ask for new items
while True:
    try:
        new_item = input("> ")

    except:
        print("sorry please use quotes around your item.")

    else:
        if new_item.upper() == "DONE" or new_item.upper() == "QUIT":
            show_list()
            break
        elif new_item.upper() == "HELP":
            show_instructions()
        elif new_item.upper() == "SHOW":
            show_list()
        elif new_item.upper() == "REMOVE":
            remove_item()
        else:
            add_item(new_item)

any insights?

1 Answer

There might be some problem, In showlist function, check the parenthesis in line 7, (print statement)

def show_list():
    # print out the list
    clear_screen()
    print("Here's your list")
    index = 1
    for item in shopping_list:
        print("{}. " + item).format(index)
        index+=1

    print("-" * 10)
    print("")

Check if it works fine. And do comment if it doesn't solves the problem.