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 trialCarly Gloge
429 PointsIt doesn't print out "Added! List has xx items." after running the code with some items (Apples, bread, etc).
It doesn't print out "Added! List has xx items." after running the code with some items (Apples, bread, etc). Can anyone let me know what's wrong with my code?
shopping_list = ()
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
new_item = input("> ")
if new_item == 'DONE':
break
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shhopping_list)))
continue
print("Here's your list:")
for item in shopping_list:
print(item)
[MOD: added ```python code formatting -cf]
Carly Gloge
429 PointsThank you Tobias! I didn't see hh in the shhopping list. Now I do:)
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsAfter reformatting your code, I noticed that the "Added!" print statement is indented too far making it part of the if new_item == 'DONE':
code block. But since it's after the break
statement, it will never execute.
Unindenting the shopping_list.append()
statement, the print
statement and the continue
(which is redundant as the last statement in the loop and may be removed), the code will execute correctly, provided you correct the typo "shhopping_list" mentioned by Tobias.
Carly Gloge
429 PointsThank you Chris! after unindenting the shopping_list.append() statement, I still get the following error in the console when I add a new item or when I type DONE.
Apples Traceback (most recent call last): File "shopping_list.py", line 12, in <module> shopping_list.append(new_item) AttributeError: 'tuple' object has no attribute 'append'
When I type DONE, I see the following sentence.
Here's your list:
Do you see what's wrong here? Thanks again!
Chris Freeman
Treehouse Moderator 68,441 PointsSorry, one additional fix I forgot to mention. shopping_list
should be initialized as an empty list
using [ ]
instead of an empty tuple
using ( )
. Here is your fully code:
shopping_list = [] # <-- initialize as list
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
new_item = input("> ")
if new_item == 'DONE':
break
shopping_list.append(new_item) # <-- unindent to align with 'if'
print("Added! List has {} items.".format(len(shopping_list)))
continue # <-- continue implied. Not needed if last statement in block.
print("Here's your list:")
for item in shopping_list:
print(item)
Tobias Mahnert
89,414 PointsTobias Mahnert
89,414 Pointsprint("Added! List has {} items.".format(len(shhopping_list)))
did you see that shhopping list has two hh?