Start New Discussion within our Software Development Community

Hello

I'm trying to make a verb conjugator for Spanish. The problem is that I need to get the last two characters in the verb to be able to determine the verb group to which it belongs.

In Spanish, conjugation depends on the verb ending, among other factors. Verbs end in either -ar, -er or -ir. So the program must be able to isolate these two letters and then use conditional structures to determine how to proceed.


But here is the problem:

verb = input("Write the verb here: ")
if verb[-2:-1] == 'ar':
    do this
elif verb[-2:-1] == 'er':
    do this
elif verb[-2:-1] == 'ir':
    do this
else:
    print('you did not provide a verb')

if verb is 'amar', verb [-2:-1] will return 'a', not 'ar'. Can you tell me a way of telling python to isolate from the second to last character up to the VERY last one?


After much thinking I came up with this:

verb= 'amar'
verb=[-2:len(verb)]
'ar'

But I don't know if this is the right wayor if this is elegant at all.

Please help me.

Also, form the habit of using a list instead of if/elif. It is more straightforward as all of the conditions are on one line. And verb[-2:] is just shorthand for verb[-2:len(verb)], just as verb[:-2] is shorthand for verb[0:-2].

verb = input("Write the verb here: ")
if verb[-2:] in ['ar', 'er', 'ir']:
#
else:
#
# or
found = False
for ending in ['ar', 'er', 'ir']:
    if verb.endswith(ending):
        found = True
Comments
good pattern

Also, form the habit of using a list instead of if/elif. It is more straightforward as all of the conditions are on one line. And verb[-2:] is just shorthand for verb[-2:len(verb)], just as verb[:-2] is shorthand for verb[0:-2].

verb = input("Write the verb here: ")
if verb[-2:] in ['ar', 'er', 'ir']:
#
else:
#
# or
found = False
for ending in ['ar', 'er', 'ir']:
    if verb.endswith(ending):
        found = True

Second case shortly can be said:

found = verb.endswith(['ar','er','ir'])
for((word  = wordlist[-2]) in ['ar','er',ir']):
    print word

Riechieking, you should test your code before posting. This can't work in python because word = wordlist[2] is not an expression but a statement. This is a C idiom, not a python one.

Comments
thanks

Riechieking, you should test your code before posting. This can't work in python because word = wordlist[2] is not an expression but a statement. This is a C idiom, not a python one.

Grib your are right. Its a C++ style. I just send that from my htc phone and there was no time to test.
I will check it up one I get home.
Cheers

Thank you all for your quick answers!

The endswith method is going to be really helpful for my project.

I'm going to test all your suggestions and will post back if more questions arise (I'm intrigued by the way you used the lists).

This question has already been answered. Start a new discussion instead.