shopping_cart = ['apples', 'oranges', 'banana', 'kiwi', 'avocado', 'peaches']
s_list = list(shopping_cart) # shopping_cart is already list so you can ignore it.
if 'mango' in s_list:
print('Done')
else:
print('Not Done')
i Think this is what you’re trying to do:
shopping_cart = ['apples', 'oranges', 'banana', 'kiwi', 'avocado', 'peaches']
if 'mango' in shopping_cart:
print('Done')
else:
print('Not done')
In your code, you’re looping through the shopping cart list and checking if that item (which it is of course) so it will just print a bunch of ‘Done’
Your issue is that you have a misunderstanding with how the code works.
for mango in s_list
creates a placeholder variable named mango, which is then assigned the value of each item in s_list
over the iterations. Since the item which is currently the value of mango
is always in the list, this part if mango in shopping_cart
always evaluates as true.
This code will check each item if it’s a mango:
shopping_cart = ['apples', 'oranges', 'banana', 'kiwi', 'avocado', 'peaches']
s_list = list(shopping_cart)
for item in s_list:
if item == 'mango':
print('Done')
else:
print('Not done')
If you just want to know if there’s a mango in the cart, you can just skip the loop and do it like this:
if 'mango' in shopping_cart:
print('Done')
else:
print('Not done')