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 trialAbdullah Jassim
4,551 PointsIm trying to do this exercise in different ways, please advise whats wrong with my code. I get a While True: invalid syn
shopping_list = []
def help():
print("These are the instructions for using the shopping list")
print("Type HELP to print out instructions")
print("Enter DONE to stop adding items")
while True:
list = input("> ")
shopping_list.append(list)
if list = 'DONE':
break
elif list = 'HELP':
help()
print("Here is your list: ")
for list in shopping_list:
print(list)
SyntaxError: invalid syntax
treehouse:~/workspace$ python sshopping_list.py
File "sshopping_list.py", line 9
While True:
2 Answers
Steve Hunter
57,712 PointsHi there,
One issue is here:
if list = 'DONE':
break
elif list = 'HELP':
help()
You compare using double-equals ==
. Change that and your code runs.
if list == 'DONE':
break
elif list == 'HELP':
help()
Also, I'd append
to the list after doing the test for 'DONE' or 'HELP' else those words will be added to the list:
while True:
list = input("> ")
if list == 'DONE':
break
elif list == 'HELP':
help()
shopping_list.append(list)
I hope that helps.
Steve.
Jon Mirow
9,864 PointsHi there!
As far as I can see, the problem is actually in your if statements - incorect number of "="s.
Also, have a look at how you handle input like HELP and DONE - should they end up in the list?
Hope it helps!
Abdullah Jassim
4,551 PointsAbdullah Jassim
4,551 PointsThanks... so I did add the append function but it still does include the DONE. I tried to put it outside the While loop and inside. It still prints out DONE.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThe output of my code produces this:
The code has the
append
inside the loop, which is where it needs to be, else nothing inside the loop will be added, except for the last loop, i.e. the list will just contain DONE.With the code like the code below, the loop gets left & the append method doesn't get called if DONE is entered.
Steve.