0

I've trying to make it work without if statement with python in which it does work most of time until it reaches a word like 'celebration'. I'm not understanding what's causing this to happen.

common_suffix = ['ly', 'ion', 'ed', 'able']
def ends_in_common_suffix(word):
    for suffix in common_suffix:
        last_word = word[-len(suffix)]
        return last_word < suffix
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Eyobel
  • 1
  • 1
  • 1
    Also [Check if string ends with one of the strings from a list](https://stackoverflow.com/questions/18351951/check-if-string-ends-with-one-of-the-strings-from-a-list) – kaya3 Feb 24 '20 at 00:56
  • How can it ever consider the second suffix? – Peter Wood Feb 24 '20 at 00:58
  • Sorry, if this sounds like a stupid question, but why wouldn't it consider the second suffix? – Eyobel Feb 24 '20 at 01:00
  • Your `return` is inside the loop. Consider `any(word.endswith(suffix) for suffix in common_suffix)` if you only need to know if a words ends thus, or move the return out of the loop (not sure if that's the only problem). – Jongware Feb 24 '20 at 01:09
  • word[-len(suffix)] will grab the letter at the `-index` of the length, so for instance 'ly' is len 2, so if my word was 'suffix' the line `last_word = word[-len(suffix)]` would set `last_word` to 'i' (second to last letter). To make this work you can do something like `def ends_in_common_suffix(word): matches = [word.endswith(suffix) for suffix in common_suffix] return any(matches)` – Dash Winterson Feb 24 '20 at 01:13
  • Thank you for all the support! The code finally worked. – Eyobel Feb 24 '20 at 01:49

0 Answers0