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 Basics (2015) Shopping List App Shopping List Introduction

Abdullah Jassim
Abdullah Jassim
4,551 Points

Im 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

Hi 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.

Abdullah Jassim
Abdullah Jassim
4,551 Points

Thanks... 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.

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("> ")

  if list == 'DONE':
    break
  elif list == 'HELP':
    help() 

shopping_list.append(list)    
print("Here is your list: ")    
  print(list)

The output of my code produces this:

============== RESTART: C:\Users\steve\Desktop\shopping_list.py ==============
Type HELP to print out instructions
Enter DONE to stop adding items
> bread
> apples
> eggs
> DONE
Here is your list: 
bread
apples
eggs
>>> 

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.

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("> ")

  if list == 'DONE':
    break
  elif list == 'HELP':
    help()

  shopping_list.append(list)


print("Here is your list: ")    

for list in shopping_list:
  print(list)

Steve.

Hi 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!