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
Hunter G
6,612 PointsShopping_List_4.py practice. trying to add new commands. .
Hey everybody, was wondering why when i try to add " or 'DELETE' " to the elif new_stuff statement on this same line as 'REMOVE' it basically gives me an error after i type in the first items on a list. as soon as i type a few items in when i run the code, it immediately prompts "Which item? Tell me the number." even though i didn't type in 'REMOVE' or 'DELETE'. do i need to just add a new elif statement that has 'DELETE' or is it possible to use " or 'DELETE' " ?? if anybody could solve this problem it would be most helpful! Here is my code below.
new_stuff = input("> ")
^^^PS. also i was wondering if anybody could explain what this line of code means exactly.. I either missed it or Kenneth didn't explain it too well : / What i'm not understanding is the " input("> ") part. I understand that you're assigning an input to the value of the new_stuff variable, but what does the " > " have to do with it? What exactly is it doing? Thanks again!!!!
shopping_list = []
def remove_item(idx):
index = idx - 1
item = shopping_list.pop(index)
print("Removed {}.".format(item))
def show_help():
print("\nSeparate each item with a comma.")
print("Type DONE to quit, SHOW to see the current list, REMOVE to delete an item, and HELP to repeat this message.")
def show_list():
count = 1
for item in shopping_list:
print("{}: {}".format(count, item))
count += 1
print("Give me a list of items you want to shop for.")
show_help()
while True:
new_stuff = input("> ")
if new_stuff == 'DONE':
print("\nHere's your list:")
show_list()
break
elif new_stuff == 'HELP':
show_help()
elif new_stuff == 'SHOW':
show_list()
continue
elif new_stuff == 'REMOVE' or 'DELETE': #having a problem here . .
show_list()
idx = input("Which item? Tell me the number. ")
remove_item(int(idx))
continue
else:
new_list = new_stuff.split(",")
index = input("Add this at a certain spot? Press enter for the end of the list, "
"or give me a number. Currently {} items in the list.".format(
len(shopping_list)))
if index:
spot = int(index) - 1
for item in new_list:
shopping_list.insert(spot, item.strip())
spot += 1
else:
for item in new_list:
shopping_list.append(item.strip())
[MOD: added ```python formatting -cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsAnswering the first part. What's up with:
new_stuff = input("> ")
The command input() simply prompts the user for input and returns the value as a string (in Python 3) or raw (in Python 2). The value between the parens is a simple string to use as a prompt for the user. It could easily have been:
new_stuff = input("Enter your response here: ")
It's a good practice to include some whitespace at the end of the string to give space before the user's cursor entry point.
Looking at your code now...
Edit: One of your elif statements is probably not what you want. The following says: "If the variable new_stuff is equal to the string 'REMOVE" or True". So it will always enter this code block.
elif new_stuff == 'REMOVE' or 'DELETE': #having a problem here . .
Since a non-empty static string is always "truthy", the string 'DELETE' is always considered true.
What you probably want is:
elif new_stuff == 'REMOVE' or new_stuff == 'DELETE':
Sebastian Röder
13,878 PointsYou could also write this line slightly less verbose like this:
elif new_stuff in ('REMOVE', 'DELETE'):
# more code
This checks whether new_stuff is equal to one of the strings in the tuple ('REMOVE', 'DELETE'). An added bonus is that you can easily add more strings to that list later without repeating the new_stuff == ... over and over again:
elif new_stuff in ('REMOVE', 'DELETE', 'DESTROY', 'KABOOM'):
# more code
Hunter G
6,612 PointsWanted to say thank you both for your timely replies. I highly appreciate it! See you around in the forums! :)
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsSolved. See answer.