Endswith() with user input

Every tutorial I’ve seen online all talk about strings that are already in the code. I’m a beginner trying to make a simple Spanish verb conjugator. I’m trying to make it so that when the user enters an infinitive verb (ex: caminar) the last two letters are taken off so that the verb can be conjugated later on.

Not much but here’s what I’ve got so far:

def Choose_Option():
original = input (‘Enter an infinitive verb:’)
if input.endswith(‘ar’):
def trim(original):
return original[ : -2]

Hi @redpotter - welcome to the forum.

You’re certainly on the right lines, but you’ve made a few mistakes. :slight_smile:

Here’s what I think your code should look like, correctly formatted:

def Choose_Option():
    original = input("Enter an infinitive verb: ")
    if input.endswith("ar"):
        def trim(original):
            return original[:-2]

Let’s start at the top and work down…

>>> Enter an infinitive verb: test
Traceback (most recent call last):
  File "verb.py", line 7, in <module>
    Choose_Option()
  File "verb.py", line 3, in Choose_Option
    if input.endswith("ar"):
AttributeError: 'builtin_function_or_method' object has no attribute 'endswith'
>>>

The interpreter doesn’t like input.endswith("ar")… because input is a built-in function. You should be calling the .endswith() method on a string variable. Can you see how to change this to fix the error?

If I fix that error, and try again, then the program runs to completion but I get no output from the console. You have a bit more work to do, I think. :slight_smile:

2 Likes

Great thank you. I’ll have to figure out how to complete the output from the console now.

OK so I have no errors now but the console still won’t output the verb with the last 2 letters taken off which is what I’m trying to get :face_with_raised_eyebrow:

Ok. Can you post the updated program, as you now have it?

When you’re posting the code, please make sure to use the code formatting button ( </> ) in the editor.
capture
The </> button will insert a block into your post, like this:
capture_r

Paste the code in the middle, where it says type or paste code here. :slight_smile:

You’ll end up with code like this:

def some_stuff():
    do something

:slight_smile:

Here it is:

def Verb_Choice():
    original_verb = input ("Enter an infinitive verb: ")
    if original_verb.endswith("ar"):
        def trim(original_verb):
            return original_verb[:-2]

while True:
    print ("")
    original_verb = Verb_Choice()
    print ("")