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 trialSam Bui
878 PointsMy 'HELP', 'SHOW', 'DONE' and 'QUIT' don't work
Hi, I am writing the script for shopping_list3.py in 'Shopping list take three' video, I follow every steps that Kenneth did. Everything works just fine, except when I want to quit the program when I type 'DONE' or 'QUIT', it doesn't work but instead it add 'DONE', 'QUIT' to the list.
The similar issue also happen with 'HELP' and 'SHOW' command
I have double checked my code and couldn't find any thing, I would be very appreciated if you can show me what I did wrong.
Thank you
import os
shopping_list = []
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def show_help():
clear_screen()
print('''
type 'DONE' to stop the app
type 'SHOW' to see what current in the list
type 'HELP' to know more info about special functions, commands.
''')
def show_list():
clear_screen()
print('here is your list.')
index = 1
for item in shopping_list:
print('{}. {}'.format(index, item))
index += 1
print('-'*10)
def add_to_list(new_item):
show_list()
if shopping_list:
position = input('where should I add {}?\n'
'press enter to add to the end of the list'
'> '.format(item))
else:
position = 0
try:
position = abs(int(position))
except ValueError:
position = None
if position is not None:
shopping_list.insert(position-1, new_item) # why position-1, is it supposed to be +1?
# oh, I think I get it, because position will put new_item in actual index so.
# Even though we call index start at 1, the actual index start at 0, so our index will alway have more 1 unit than actual index
# Therefore, if you assign your next item in number 2, the actual index will be 1, which is your assigned index (position) -1 > 2-1 = 1.
else:
shopping_list.append(new_item) # I aslo don't get this part
show_list()
show_help()
while True:
new_item = input('> ')
if new_item.upper() == 'SHOW':
show_list()
continue
elif new_item.upper() == 'HELP':
show_help()
continue
elif new_item.upper == 'DONE' or new_item.upper == 'QUIT':
break
else:
add_to_list(new_item)
show_list()
2 Answers
Josue Ipina
19,212 PointsOn your last elif condition, you forgot to add the parenthesis on new_item.upper(). Try fixing that and check if it breaks the loop correctly.
Also in your add_to_list() function:
def add_to_list(new_item):
show_list()
if shopping_list:
position = input('where should I add {}?\n'
'press enter to add to the end of the list'
'> '.format(item)) #<-- the format variable should be new_item, as *item* is not defined
Sam Bui
878 PointsHi Josue, It works now, thank you for your help, I indeed overlooked those details, it seems small but enough to mess up my script :)