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

Jamal Scott
Jamal Scott
9,656 Points

Python Basics

This challenge is a little different. You shouldn't have to write very much code. You probably recognize this code. It's pretty much our shopping_list_2.py from before. I want you to help me with a little refactor, though. Refactoring is when we change code to make it better, often by creating new functions. Create a new function named main that doesn't take any arguments. Move everything from line 22 (show_help()) and below into your new function. You shouldn't have any code that isn't inside of a function.

Keeps saying "Bummer! Make sure all your code is in a function"

I literally wrote my main function above show_help() and indented show_help

13 Answers

Darryn Smith
Darryn Smith
32,043 Points

I had very weird results too. I was sure I did everything right. After reading this thread, I added a blank line between the 'def main()' declaration and the 'show_help()' command. I changed no indentation or anything, just added a line, and suddenly it all checked out.

Weird.

Rajagopal Venu
Rajagopal Venu
1,077 Points

I was stumped too, but this worked for me. Thanks!

Hi Jamal,

I'm not certain what went wrong there; it really is as simple as def main(): and then making sure everything below line 22 is indented. Did you include the show_list(shopping_list) in your indent? Below is a functional answer for this Challenge.

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)
Jamal Scott
Jamal Scott
9,656 Points

lmao i literally just added the main function then indented show_help i did nothing else im not sure whats wrong. thnx for your help

Zeljko Porobija
Zeljko Porobija
11,491 Points

Yep, that passed the challenge. However, if you put this code into some IDLE Python, this won't work. Why? Because you need to call the main function. So, you need to write another line, unindented, main() and then the things run smoothly. But it doesn't pass the challenge then. It's really weird.

Upon playing with this Challenge a bit, it looks like what is throwing students off while completing this Challenge is that there is, by default, whitespace on the line following return shopping_list. If def main(): is put on line 22 without deleting that whitespace, the Challenge will not pass. Additionally, if you press enter while on line 22, it will create a newline with whitespace which will need to be deleted for the Challenge to pass as well. I've reported this as a bug, as whitespace in that location didn't cause a problem when I tested the Challenge code in Workspaces or locally on Python 3.5.1.

In the meanwhile, deleting that whitespace should allow you to pass the Challenge otherwise.

Thomas Souza
Thomas Souza
7,943 Points

the interface suffers similar issues on most if not all challenges.

I can't get the quiz to accept any answer - just tried all of the suggestions listed above. There is no way that my answer is incorrect.

Hi amanda,

could you post your code?

I'm having the same issue. This is a buggy quiz that should be taken out or redone imo. Anyone else have issues trying to tab multiple lines in the environment?

The Challenge editor doesn't allow for multi-line tabbing by selecting the lines and pressing tab, if that's what you're trying to do, but you really only need a single tab on the lines 'show_help', 'shopping_list', 'while', and 'show_list', because the tab in the Challenge only provides two spaces - and everything else is tabbed by four spaces.

don't forget to indent everything afterwards.

David Yap
David Yap
2,009 Points

Redo this in pycharm and it works.

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()

    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)
Will Hunting
Will Hunting
13,726 Points

Cheers Yap, it's ashame the environment we are being provided complicates the simplest of tasks.

Will Hunting
Will Hunting
13,726 Points

In a nutshell I am displeased with this task because it runs fine with a single indentation after 'show_help()' before 'def main():' in Pycharm IDE. Which suggests there is a fault with the environment which we are being provided.

Mohhamed Syed
Mohhamed Syed
2,017 Points

dont forget to indent your comments it...is...an...annoyance

Thomas Souza
Thomas Souza
7,943 Points

if a specific browser works best for the environment, it should be specified.

Peter Rzepka
Peter Rzepka
4,118 Points

but you really only need a single tab on the lines 'show_help', 'shopping_list', 'while', and 'show_list', because the tab in the Challenge only provides two spaces - and everything else is tabbed by four spaces. THIS HELPED

Will Hunting
Will Hunting
13,726 Points

Would be nice if a mod could help out here....

Manuel Gapp
Manuel Gapp
8,820 Points

This is still buggy - would so be so kind and fix this. Didn't work for me until I removed the comment:

make a list to hold onto our items

Else I would always get the "Can't see the help-menu"-Error.

Bruno Melo
Bruno Melo
565 Points

david yap Your code sure passes the quiz but I tried it on @repl.it and it won't work. I also tried the other suggestions above and still won't work.

Even so, the quiz was passed.