lundi 31 août 2015

Why is this "in" statement not evaluating to True?

I have this code here

input1 = input("Toppings: ")
split = input1.split("|")
length = len(split)

for i in range(0, length-1):
  if "cheese" in str(split[i]):
    del split[i]

s = "|"
print(s.join(split))

and it's meant to take a string, split it by "|" into an array, then search the array for the string "cheese", and if it finds it; take it out of the array, and then print out the parsed array.

When inputting this:

Toppings: ham|lettuce|cheddar cheese|sliced cucumber|tomato
ham|lettuce|sliced cucumber|tomato

The output is correct - it removes the element that has "cheese" in it from the array. However, when inputting this:

Toppings: egg|lettuce|cheese spread|mac'n'cheese
egg|lettuce|mac'n'cheese

It doesn't remove it from the array. When I looked up the in operator it said it didn't match whole word only so I thought it would work. What would be the correct code to put into the if statement so it would detect "cheese" in "mac'n'cheese"?

Aucun commentaire:

Enregistrer un commentaire