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 Basics (2015) Shopping List App Refactor

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

Trouble with "main" function

I made a 'main' function and put all those things in it, but this doesn't seem to be what it wants.

def show_help():
    # print out instructions on how to use the app
    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 show_list(shopping_list):
    # print out the list
    print("Here's your list:")

    for item in shopping_list:
        print(item)

def add_to_list(shopping_list, new_item):
    # add new items to our list
    shopping_list.append(new_item)
    print("Added {}. List now has {} items.".format(new_item, len(shopping_list)))
    return shopping_list

def main():
    show_help()

    # make a list to hold onto our items
    shopping_list = []

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

        # be able to quit the app
        if new_item == 'DONE':
            break
        elif new_item == 'HELP':
            show_help()
            continue
        elif new_item == 'SHOW':
            show_list(shopping_list)
            continue
        add_to_list(shopping_list, new_item)

    show_list(shopping_list)

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

Your code, as is, passes for me in the challenge. Please try it again.

I tried this as well, and it didn't pass. Same exact code.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

Grr. My only problem with Python is that I keep fighting with whitespace issues. I thought it was supposed to be easier this way rather than curly braces and semicolons?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

My biggest recommendation is to always use 4-space indentation and not never use Tabs. Most editors allow you to set this preference. Cutting and pasting can exaggerate spacing issues with mixed spaces and tabs. Also most modern editors will dynamically flag indention issues as you type. But as you get better at Python, bad indentation becomes very obvious and your Python-OCD will kick in! I wouldn't worry about it too much. It become second nature very quickly.

I am pretty sure I figured out the issue. After adding the def main(): above show_help(), you then highlight from show_help() and below, and then pressing shift+tab to move everything over under the main() function. For some reason, certain lines aren't tabbed correctly, so you have to really look and see where these errors occur. As below, this is what happens:

def main():
    show_help()

    # make a list to hold onto our items
    shopping_list = []

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

      # be able to quit the app
      if new_item == 'DONE':
        break
        elif new_item == 'HELP':
          show_help()
          continue
          elif new_item == 'SHOW':
            show_list(shopping_list)
            continue
            add_to_list(shopping_list, new_item)

            show_list(shopping_list)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Good catch. I'm running on Ubuntu. Perhaps my cut-and-paste behaves differently.

Also to add, as I just realized this as well; to indent highlighted-selected code, it is better to use ctrl + ]. This will indent everything properly. As far as, the cut-and-paste, I don't know if it has to do with browsers, but in dealing with mozilla based ones, during the challenges, I have found that if you hold the right clicker on the mouse for a second, there will be a window with cut, copy, paste, etc options. Weird stuff... Hope this all makes sense.

It is the indent and that is all the ctrl + ] works

This is reason enough to never use this language.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Sorry you're finding this so frustrating. Please don't confuse the very basic Treehouse Workspaces IDE limitations with the language itself. I would say it might be a reason to never use workspaces (which I don't because I practice all my development off-line in a local IDE).

Python can seem strange if you're coming from a language that uses a lot of parentheses or braces to limit code blocks. But the cleanness of the code in python comes quite naturally after a while. Then when you go back to visit the other languages they might start to seem cluttered.

Best of luck in whatever language you choose to move forward with.

Craig, I agree with Chris. I am still at a very beginning level with programming and originally started with C. That is a language that requires a lot of discipline; it is quite difficult to learn. I dipped my feet in for a little while to learn, but realized python was better for me to start with to give me more momentum in learning programming. Anyway, your eyes will get used to the differences of syntax. As far as using workspaces, eh... it's okay, but can be glitchy. You should learn to use vim, as it is super efficient. Bring it up while going through the tutorial videos.

Worrying about white space (which is often ignored in programming languages) when it affects the functionality versus style is something I simply do not agree with. I have used vim before. I have not used Python. For some reason I always avoided it. Now, I have a reason.

Thanks Chris. I've had to work with several over the years.

Whatever is your preference. Though I'm sure you know, I do want to remind you that when white space is ignored in other languages, it can be used for style, BUT I would say what matters is that white space is used for readability IN ALL LANGUAGES; it's easier on the eyes. Python emphasizes readability and the white space matters for functionality, but also allows for better readability.

I hope not to offend you, but you are simply criticizing a language just because of your frustration with white space, and it is very easy to avoid. All you have to do is either hit the space bar four times (which is annoying, so forget that), or make sure your tab setting is set to 4 spacings in (which is way less tedious) and hit tab once.

This isn't necessary. In fact, I said "style" that would imply readability and organization. Sorry I don't like the language.