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

Self practice on basic shopping list. Can you please provide direction on whats wrong with the code?

shopping_list = []



def main():
  list = input("What do you want to buy? ")
  list = input("Type DONE to quit app ")

  if list == 'DONE':
    break

  else:
    shopping_list.append(list)
    continue

  print(list)

  main()

I am going through past questions trying to remember the question and solve them again without any help. I may have missed a few instructions but I want to make sure my logic and syntax are right. Can you please advise.

2 Answers

Use break and continue in loops, for functions we have recursion(calling itself again) and return. See the example below.

def main():
  list = input("What do you want to buy? ")
  list = input("Type DONE to quit app ")
  if list == 'DONE':
    return
  else:
    shopping_list.append(list)
    main()
  print(list)
Abdullah Jassim
Abdullah Jassim
4,551 Points
shopping_list = []

def help():
  print("These are your instructions")


def main():
  list = input("What do you want to buy? ")
  list = input("Type DONE to quit app ")
  list = input("Type HELP to print instructions ")


  while True:
    list = input("> ")

    if list == 'DONE':
      return
    elif list == 'HELP':
      help()
    else:
      shopping_list.append(list)
      main()

    for list in shopping_list:
      print(list)

This time, I used the function method but when I run the code, I get a blank line. And when I type in DONE i get - bash: syntax error

Indentation is the problem here. before while loop and the line " list = input("Type HELP to print instructions ")".

According to the given code, while loop exists outside the main function. And like i mentioned, return is to be used inside the function, whereas to break a loop we use, 'break'