Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

I'm getting Traceback errors when I run the program and give a number where I would like something on my shopping list.

This is my program:

import os

shopping_list = []

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

def show_help():
    clear_screen()
    print("What should we pick up at the store?")
    print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' for this help.
Enter 'SHOW' to see your current list.
""")


def add_to_list(item):
    show_list()
    if len(shopping_list):
        position = input("Where should I add {} in?\n"
                         "Press ENTER to add to the end of the list\n"
                         "> ".format(item))
    else:
        position = 0

    try:
        postion = abs(int(position))
    except ValueError:
        position = None
    if position is not None:
        shopping_list.insert(position-1, item)
    else:
        shopping_list.append(new_item) 
    show_list()

def show_list():
    clear_screen()

    print("Here's your list:")

    index = 1
    for item in shopping_list:
        print("{}. {}".format(index, item))
        index += 1

    print("-"*10)


show_help()

while True:
    new_item = input("> ")

    if new_item.upper() == 'DONE' or new_item.upper == 'QUIT':
        break
    elif new_item.upper() == 'HELP':
        show_help()
        continue
    elif new_item.upper() == 'SHOW':
        show_list()
        continue
    else:
        add_to_list(new_item)



show_list()

This is what I get in the console:

Here's your list:                                                                                                           
1. apples                                                                                                                   
2. bananas                                                                                                                  
3. pizza                                                                                                                    
----------                                                                                                                  
Where should I add pears in?                                                                                                
Press ENTER to add to the end of the list                                                                                   
> 2                                                                                                                         
Traceback (most recent call last):                                                                                          
  File "shopping_list_3.py", line 64, in <module>                                                                           
    add_to_list(new_item)                                                                                                   
  File "shopping_list_3.py", line 32, in add_to_list                                                                        
    shopping_list.insert(position-1, item)                                                                                  
TypeError: unsupported operand type(s) for -: 'str' and 'int'                                                               

3 Answers

Did you understand the error? It is saying here:

shopping_list.insert(position-1, item)

you are trying to subtract an integer from a string which is not allowed in Python.

You know your code attempts to convert position to an integer so that would be the next line to check. With the misspelling the integer version of position is stored in postion while position remains a string.

You have misspelled position here:

try:
        postion = abs(int(position))

Okay. I hope I start getting better at catching my errors soon. Thanks!

Okay yes I understand. I will check for spelling first when I'm stuck the next time. Thanks.