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 Introducing Lists Build an Application Display the List

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

the teacher he could have added '''add_to_list''' to an else statement, but it meant that he wouldn't able to show me t

the teacher he could have added '''add_to_list''' to an else statement, but it meant that he wouldn't able to show me the '''continue''' function. Can anyone please show me it would have looked like if the teacher used else statement please?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

By extending the if statement with the code:

    else:
        add_to_list(new_item)

the if statement becomes the last code within the while block. Therefore it will not matter if the continue statements remain. In either case, the code will jump back to the top of the while loop. Since, it doesn’t matter, the continue statements should be removed to simply the code.

Post back if you need more help. Good luck!!!

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

in your python code, could you please include were break and continue would have been. I'll copy and paste the code here so save you from typing it from scratch.

shopping_list = []
#create a new emtpy list named shopping_list 


def show_help():
    print("what should we pick up at the store. ")
    print("""
Enter 'DONE' when you are finished entering items
Enter 'HELP' for this help
Enter 'SHOW' to show list
""")


def add_to_list (item): 
    shopping_list.append(item) 
    print("item has been added, the current list is at {}.".format(len(shopping_list)))


def show_list():
    print("heres your list")
    for shopping_lis in shopping_list:
        print("* " + shopping_lis)

show_help()
while True:
    new_item = input("> ")


    if new_item == "DONE":

        break 

    elif new_item == "HELP":
        show_help()
        continue
    elif new_item == "SHOW":
        show_list()
        continue

    add_to_list(new_item)  

show_list() ```
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Original code:

while True:
    new_item = input("> ")

    if new_item == "DONE":
        break
    elif new_item == "HELP":
        show_help()
        continue
    elif new_item == "SHOW":
        show_list()
        continue

    add_to_list(new_item)  

show_list()

becomes

while True:
    new_item = input("> ")

    if new_item == "DONE":
        break 
    elif new_item == "HELP":
        show_help()
    elif new_item == "SHOW":
        show_list()
    else:
        add_to_list(new_item)  

show_list()
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

hi, on both the ''elif'' statements its missing the key word '''continue''', on typing '''SHOW''' & '''HELP''' wouldn't it add SHOW & HELP onto the list? As in the original code with out continue it added them to the list.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

In an if/elif/else statement, only exactly one code block will execute. Since the add is in the else block, it will only execute if none of the preceding conditions are met.

The next line to execute after show_help or show_list will be the while statement.