Https://www.codecademy.com/courses/learn-python-3/lessons/introduction-to-strings/exercises/strings-in-conditionals-ii

#Not sure why its not working , open to suggestions thanks.

def contains(big_string,little_string):
for letter in big_string:
if letter in little_string:
return True
else:
return False
print(contains(‘watermelon’, ‘melon’))

Hello @mega6374850963, welcome to the forums! This code:

will loop through the big_string, in this case, ‘watermelon’. The first character of watermelon is w. letter is therefore w for the first iteration. The next line then executes:

Is w in melon? If it is, return True, else return False. Since w isn’t in melon, the function returns False.


Conversely, if you had `contains("apple", "at")`, it would return `True`, since the character `a`(the first character of `apple`) is in both `at` and `apple`.

I hope this helps!

ok than thank you i actually got it to work i used:
if “melon” in little_string:
return True
else:
return False
print(contains(“watermelon”, “melon”))
print(contains(“watermelon”, “berry”))

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.