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 trialRobert Baucus
3,400 PointsSecond Shopping List App (finding indentation error!)
So I have been pulling my hair out trying to find the indentation error in this program. I am using Notepad++ and running it through the command line in Windows 10. As far as I can tell, the indentation lines up, but Python does not think so. The code:
print("Let's make a shopping list, breh!")
print("Add new items one at a time!")
shopping_list = []
def show_list():
print("Here's your shoping list, breh!")
for item in shopping_list:
print(item)
def showHelp():
print("""
If you would like to add something to the list type it in and press enter, otherwise
type 'DONE' to exit, 'HELP' for this help, or 'SHOW' to show the contents of the list """
)
def add_new_item(new_item):
shopping_list.append(new_item)
print("{} added to shopping list. There are now {} items in your list.".format(new_item, len(shopping_list)))
show_help()
while True:
if new_item == 'DONE':
break
elif new_item == 'HELP':
show_help()
continue
elif new_item == 'SHOW':
show_list()
continue
add_new_item(new_item)
show_list()
If you run it , the following error will show up (Python 3.6):
"File "shopping2.py", line 20 print("{} added to shopping list. There are now {} items in your list.".format(new_item, len(shopping_list))) ^ TabError: inconsistent use of tabs and spaces in indentation"
Does anyone know any tricks for how to find indentation errors or where I can go to learn more about this, I am super frustrated!
2 Answers
Jon Mirow
9,864 PointsHi there!
To troubleshoot this In notepad++, go to View, then hover on Show Symbol, then click "Show whitespace", you'll see the difference between spaces and tabs:
spaces:
if.name.==."bob":
....print("Hi {}".format(bob)
tabs:
if.name.==."bob":
--->print("Hi {}".format(bob)
Best thing to do when developing python is to set tabs to insert 4 spaces. In notepad ++ you can convert all your tabs to spaces by clicking Edit, hovering on Blank Space and then clicking TAB to Space. To make that the default, click Settings, then Open Preferences, and click "language" on the left hand menu. Then you'll see tab settings on the right of the box. Select python from the scroll list on the right and click tabs to spaces :)
hope it helps :)
P.S. watch your variable names ;)
Robert Baucus
3,400 PointsTHANK YOU! Wow, showwhitespace is immediately propelled into my top 100 list of favorite things!