13/14 Need some guidance here


#1

I saw another use expand their code to add some user input functionality. I decided to take it one step further and add some error detection. I would like to have python check to see if the user inputs anything at all and if that option is in the list. Here is my code, how would I have python check if the input is in the list?

backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
print backpack
delete = raw_input('What would you like to remove from your backpack?')
if len(delete) > 0 and 
    item = delete.lower()
    backpack.remove(item)
    print backpack
else:
    print "You did not make a valid selection!"

#2

after end put: delete in backpack
This check if item (in this case word in variable 'delete" is in list.
To check if something is not in the list use: item not in list


#3

Hi @ryanesk,

There is no need for len(delete) > 0 as the following condition will have the else take care of empty input. We can also get rid of item = delete.lower() line by using .lower() on the raw_input() instead

Here's a link to the Python Doc that refers to expressions and more specifically to the in/not in comparison operator we are using.

backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
print backpack
item = raw_input('What would you like to remove from your backpack?').lower()

if item in backpack:
    backpack.remove(item)
    print backpack
else:
    print "You did not make a valid selection!"

Have fun! :smile:


#4

Thanks for the replies, guys! I think I finally got it to do what I wanted. This is why I love Codecademy!