0

I am trying to write an if, elif else clause, so that depending on the German word ending, we can see is it should go with der, die or das.

Here is my code:

word = input ("Enter word: ")
if (word.endswith('er' 'ismus')):
    print ("der")
elif (word.endswith('falt' 'heit' 'keit' 'schaft' 'ung')):
    print ("die")
else (word.endswith('chen' 'lein')):
    print ("das")

I have also tried using suffix with square brackets but everything goes grey when I do that and so I can assume it won't work. And clearly true and false are not adequate responses for what I need. Is there anything else I can try?

Thanks in advance!

Frieder
  • 1,208
  • 16
  • 25
e007q
  • 1
  • 1
  • You should first to some tutorials for python basics. You always should use a list [] if you want to assign multiple strings in a variable. Also as the really good answer explained you need a loop to check for each suffix seperatly. – Frieder Oct 15 '21 at 07:39

1 Answers1

1

The endswith method really only checks if the word ends with one thing, but you can do something like:

def word_ends_with_one_of(word, options):
    for option in options:
        if word.endswith(option):
            return True
    return False

Then call that with:

suffix_die = ['falt', 'heit', 'keit', 'schaft', 'ung']
suffix_der = ['er', 'ismus']
suffix_das = ['chen', 'lein']

if word_ends_with_one_of(word, suffix_die):
    print ("die")
elif word_ends_with_one_of(word, suffix_der):
    print ("der")
elif word_ends_with_one_of(word, suffix_das):
    print ("das")

As an aside, your else clause is currently problematic, it should not have a condition attached to it (unless it's a typo and you meant to have an elif instead).


Now, even though that you be a useful function to have for other purposes, you may want to consider a more application focused method since you'll be introducing a function anyway. By that, I mean something more closely suited to your specific needs, such as:

def definite_article_for(word):
    # Could also use word_ends_with_one_of() in here.

    if word.endswith('er'):    return 'der'
    if word.endswith('ismus'): return 'der'
    if word.endswith('falt'):  return 'die'
    :
    if word.endswith('lein'):  return 'das'
    return None
}

Then use article = definite_article_for(my_word) to get the article you want.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953