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 Challenge Question

Please help, i'm kina ugggghh lol.. I've been stuck on this one for weeks. I get the following error: "Make sure all of your code is inside of a function"

Challenge: 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.

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)

3 Answers

Make sure the indentation is precise, looks like the show_help method isn't indented the same as the code below it. Everything should be nested exactly one tab in from its parent.

Hi, thank you for the response. Your answer is the exact same answer that I get online / Google, but my human-mind still can't seem to figure it out. Indention in Python = 4 spaces so in this case, if indent as expected, everything goes insides the def main(): with an indention (easier said than done). What about this? I get the same error.

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)

If there are multiple blocks nested inside each other they'll need their own indentation, so everything in the main function will be indented and then within that, everything in the while loop will be indented one further. Your original answer looked closer. There was an extra space before show_help, and possibly an issue at the end with show_list(shopping_list), I can't remember if the exercise intended that to be in the while loop or not, but if it does it would need to be indented one more tab.

Hard to say looking at the second code snippet you shared, make sure the entire code block in forum posts is surrounded by backticks plus the language name, click the markdown cheatsheet for an example.