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 (Retired) Shopping List Shopping List Project

Can't make script run completely unless I complete the if/else statement

Hello all!

Great tutorials by the way, keep up the good work!

Got a question that I can't sort out on my own:

If I copy Ken's script exactly, when executing it, I get the prompts for the items and the can input 'DONE' and get the "The items on your list are:"... but nothing else.

BUT:

If I complete the if statment with an else statment... the whole thing runs like Ken's (properly :)). And Ken's does not include any else statment whatsoever!

Can anybody explain that?

Many thanks!

4 Answers

Anthony Liu
Anthony Liu
11,374 Points
shopping_list = 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(shopping_list)))
    continue

print("Here's your list:")

for item in shopping_list:
    print(item)

This is my code for the shopping_list.py

I would guess the reason you needed to add else is because you had an indentation was off.

to write your code in a specific programming language you can

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

        ```python
        my_name = "Anthony" 
        ```
Anthony Liu
Anthony Liu
11,374 Points

Maybe you made a mistake somewhere.

It would be helpful if you were to show your code

Hi Anthony!

Many thanks for taking your time to answer this.

This is the code I had typed (I can't show the indentations, but they are there!):

shopping_list = 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

else:
shopping_list.append(new_item) print("Added! List has {} items.".format(len(shopping_list))) continue

print("Here's your list: ")

for item in shopping_list: print(item)

The funny thing is that I believe Kenneth's did not include the else statement. I think I have it just the same otherwise.

And yes, this one works for my case, so maybe I am overthinking things :)

Cheers!

Hello Anthony!

This is the code I wrote:

shopping_list = 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

  else:  
    shopping_list.append(new_item)
    print("Added! List has {} items.".format(len(shopping_list)))
    continue

print("Here's your list: ")

for item in shopping_list:
    print(item)

I tried using yours and it indeed worked perfectly. No idea what went wrong. Indentation mistakes do not always throw an error?

Nevertheless, going forward, I'll try to use your 4 space indentation instead of the default 2, it looks much clearer (it also seems to be the default on Eclipse, so I guess it is what programmers use! :))

And many thanks for the tip on posting code on the forum. I'll sure be needing that quite often.

Cheers!