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 trialJamal Scott
9,656 PointsPython 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
32,043 PointsI 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.
Evan Demaris
64,262 PointsHi 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
9,656 Pointslmao 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
11,491 PointsYep, 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.
Evan Demaris
64,262 PointsUpon 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
7,943 Pointsthe interface suffers similar issues on most if not all challenges.
pythonmanda
1,247 PointsI 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.
Evan Demaris
64,262 PointsHi amanda,
could you post your code?
Brigette Eckert
16,957 PointsI'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?
Evan Demaris
64,262 PointsThe 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.
matthew tomasetti
1,887 Pointsdon't forget to indent everything afterwards.
David Yap
2,009 PointsRedo 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
13,726 PointsCheers Yap, it's ashame the environment we are being provided complicates the simplest of tasks.
Will Hunting
13,726 PointsIn 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
2,017 Pointsdont forget to indent your comments it...is...an...annoyance
Thomas Souza
7,943 Pointsif a specific browser works best for the environment, it should be specified.
Peter Rzepka
4,118 Pointsbut 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
13,726 PointsWould be nice if a mod could help out here....
David Yap
2,009 PointsUse my answer
Manuel Gapp
8,820 PointsThis 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
565 Pointsdavid 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.
Rajagopal Venu
1,077 PointsRajagopal Venu
1,077 PointsI was stumped too, but this worked for me. Thanks!