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 (Retired) Lists Redux Shopping List Take Three

Julie Curry
Julie Curry
2,127 Points

When I try to SHOW list I receive the following error. ValueError: invalid literal for int() with base 10: 'SHOW'.

I can't figure out what I'm doing wrong. I thought it might have been the indentation, but that doesn't seem to be the problem. can someone help please.

The ValueError message that I'm getting is related to: spot = int(index) - 1 To me it looks exactly the same as it does in the video.

shopping_list = []


def show_help():
    print("\nSeparate each item with a comma.")
    print("Type DONE to quit, SHOW to see the current list, and HELP to get this message.")


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

print("Give me a list of things you want to shop for.")
show_help()

while True:
    new_stuff = input("> ")
    if new_stuff == "DONE":
        print("\nHere's your list:")
        show_list()
        break
    elif new_stuff == "HELP":
        show_help()
        continue
    elif new_stuff == "SHOW":
        show_list()
        continue
    else:
        new_list = new_stuff.split(",")
        index = input("Add this at a certain spot? Press enter for the end of the list, "
                      "or give me a number. Currently {} items in the list.".format(
            len(shopping_list)))
    if index:
        spot = int(index) - 1
        for item in new_list:
            shopping_list.insert(spot,item.strip())
            spot += 1
    else:
        for item in new_list:
            shopping_list.append(item.strip())

6 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your first instinct was correct. It is an indentation problem but one that doesn't create an indentation error. Your last if block and else block should be indented over one tab so that it falls inside the previous else clause. Take a look:

   else:
        new_list = new_stuff.split(",")
        index = input("Add this at a certain spot? Press enter for the end of the list, "
                      "or give me a number. Currently {} items in the list.".format(
            len(shopping_list)))
        if index:
            spot = int(index) - 1
            for item in new_list:
              shopping_list.insert(spot,item.strip())
              spot += 1
        else:
            for item in new_list:
              shopping_list.append(item.strip())

Right now they line up with the last else statement instead of occurring inside it. Hope this helps! :sparkles:

Julie Curry
Julie Curry
2,127 Points

Hi Jennifer,

Thanks for your response.

What you suggested is what my original code looked like.
I was getting the error, so I changed it to what is in my post.

I just changed it back to what you suggested and I still get the error.

any other idea as to what it might be?

best, Julie

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

If you can post a link to a snapshot of your workspace, I can fork it and have a look. That way, I can say more definitively. However, when I took your code and made the changes I posted, the code worked. So let's take a look at that workspace :smiley:

Julie Curry
Julie Curry
2,127 Points

Thanks again for your help, Jennifer.

Below is a link for you. It's shopping_list3.py

https://w.trhou.se/ivlxsg7r2d

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Ok, I forked your workspace and corrected the indentation for you in `shopping_list3.py'. It now compiles and runs without problem. Here's the snapshot to that space :smiley: https://w.trhou.se/iv9t3woqls

Julie Curry
Julie Curry
2,127 Points

Thanks, Jennifer. I made my indentation look like yours and I still get the error :/

Here is a link to the updated workspace. I have no idea why I'm getting this error.

https://w.trhou.se/pbu6wkamcc

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

At this point, I really don't know what to tell you. When I fork that workspace, and open the console I type python shopping_list3.py upon doing so the file compiles and runs without any error. My best advice to you is to first copy your code then create a brand new python workspace from the workspaces link at the top of the page. Make a new python file and paste that code in there. Don't forget to save! If it still doesn't work, you may need to contact support. But I still maintain that the original problem was indentation.

Julie Curry
Julie Curry
2,127 Points

hmmm...I don't have a problem at the beginning of the program, but when I type 'SHOW' I get the error that I mentioned. Do you mind attempting that to see if you get the same error?

I did what you suggested and copied and pasted and I still end up with the same error.

I appreciate you helping with this!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

No, I can run the SHOW command and produce the expected output. And when I type DONE the list prints exactly as I would expect. I have not been able to make it produce an error.

Julie Curry
Julie Curry
2,127 Points

That's so strange. I even copied and pasted your code into a different workspace :/

Uros Jevremovic
Uros Jevremovic
12,074 Points

In case I am not too late with the response, I believe the error you are encountering is happening because you are trying to use SHOW when prompt is asking you where to insert item(s) you just added to a list.

The error that you would get in that case is:

File "practice.py", line 74, in <module>

spot = int(index) - 1

ValueError: invalid literal for int() with base 10: 'SHOW'

Which I believe is what you were getting.

As long as you use SHOW command when you finished your input, and by that I mean when you added an item(s) to a list and choose the position where you want it(them) to be added, it should work fine.