user5428930
user5428930

Reputation:

Python 3.4 endswith()

I'm not exactly a Python 3.x pro but I was finding stuff to do on Python 3.x and I found this:

The third person singular verb form in English is distinguished by the suffix -s, which is added to the stem of the infinitive form: run -> runs. A simple set of rules can be given as follows:

a. If the verb ends in y, remove it and add ies

b. If the verb ends in o, ch, s, sh, x or z, add es

c. By default just add s

Your task in this exercise is to define a function make_3sg_form() which given a verb in infinitive form returns its third person singular form. Test your function with words like try, brush, run and fix. Note however that the rules must be regarded as heuristic, in the sense that you must not expect them to work for all cases. Tip: Check out the string method endswith()

So I created a code fore it but there is a problem, heres the code:

def make_3sg_form():
    Word = input('Enter a word: ')
    if Word.endswith('y'):
        New_word=Word[:+4]+'ies'
        print(New_word)
    if Word.endswith(('o', 'ch', 's', 'sh', 'x' or 'z')):
        New_word=Word[:+4]+'es'
        print(New_word)
    if Word !=Word.endswith((('o', 'ch', 'y', 's', 'sh', 'x' or 'z'))):
        New_word=Word[:+10]+'s'
        print(New_word)
make_3sg_form()

The problem is is if I do a word ending in 'y' ie lorry It will output

lorries
lorrys

Also how do I make it so the number in the code is the number of letters in the word minus 1 (just so I don't have to change the number based on the word.

Upvotes: 3

Views: 2922

Answers (2)

The6thSense
The6thSense

Reputation: 8335

making changes to your code

code:

def make_3sg_form():
    Word = input('Enter a word: ')
    if Word.endswith('y'):
        New_word=Word[:-1]+'ies'

    elif Word.endswith(('o', 'ch', 's', 'sh', 'x' ,'z')):
        New_word=Word+'es'

    else:
        New_word=Word+'s'
    print(New_word)
make_3sg_form()

output:

Enter a word:  lorry
lorries

Enter a word:  looo
loooes

Enter a word:  lklk
lklks

Notes:

You should be using if elif and else instead of just if

there is no need for or in endswith because it in default checks using or functionality

Upvotes: 3

BlivetWidget
BlivetWidget

Reputation: 11083

You need to be using if elif blocks. Multiple tests can be true and you're allowing yourself to do several of them.

Upvotes: 2

Related Questions