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 trialbrian curry
3,894 PointsCode keeps looping infinitely
shopping_list=[] new_item="" x=1
def show(): print(shopping_list)
def helpMe(): print("Special Commands are SHOW,DONE,and HELP")
new_item=input("Add an item to your list, or type DONE: ")
while new_item != "DONE": if new_item=="DONE": print(shopping_list); break
elif new_item=="SHOW":
show()
continue
elif new_item=="HELP":
helpMe()
continue
else:
shopping_list.append(new_item);
new_item=input("Add another item or type DONE: ");
for item in shopping_list: print (">"+item)
whenever you type in SHOW or HELP, code enters infinite loop, ignores continue no idea why
4 Answers
Steven Parker
231,236 PointsYour loop doesn't include the input function.
Since you input before the loop, then the loop will continue to perform the selected function infinitely.
Move the input function inside the loop so you can get a fresh user command for each iteration.
Brian Young
5,708 PointsYes, Steven is right. I would suggest going back over the video and completing the lesson, line by line, and word for word so that you get the hang of the formatting and syntax as I did. That will give you the feel for what Kenneth wants us to understand. You can then go back and put your own spin on the app if you want.
shopping_list=[] new_item="" x=1
def show(): print(shopping_list)
def helpMe(): print("Special Commands are SHOW,DONE,and HELP")
new_item=input("Add an item to your list, or type DONE: ")
while new_item != "DONE": if new_item=="DONE": print(shopping_list); break
This does not appear to be formatted properly; see my example below.
shopping_list[]
while True:
new_item = input("> ")
Start there...
Steven Parker
231,236 PointsWhat does the "shopping_list[]
" line do, and why would the "while
" line be indented more?
Brian Young
5,708 PointsWow, lol. I was just working on another post and was looking at that.
Thanks ;-)
Brian Young
5,708 PointsYes, I'm doing that now..., thanks!