-1

Write a function x_at_either_end(string) that takes a string string as a parameter, which may be empty, and returns a boolean. It returns True if and only if the string string starts or ends with the lower-case character x.

We recommend using the startswith and endswith methods of str.

def x_at_either_end(string):
    '''Returns true if string parameter starts and ends with 
    lowercase letter'''
    if (string.startswith(string, x )):
        return True
    if (string.endswith(string, x )):
        return True
    return False

print(x_at_either_end('Ping pong'))
print(x_at_either_end('pax'))
print(x_at_either_end('xposure'))
Hugo Smith
  • 15
  • 4

3 Answers3

0

It seems that you're passing the string as an argument to startswith and endswith, which is not the proper way to use those functions. In addition, you're passing the variable x as an argument when you probably want to pass the string 'x'. It should look something like this:

if string.startswith('x'):

As for the way you've written your question: it's a good idea to list the problem you're having (whether your program is encountering an exception or just doing something unexpected). You should also make sure that the title and the question are consistent (one says "starts and ends with" and the other says "starts or ends with").

If you want to read more about using startswith and endswith, look here: https://www.programiz.com/python-programming/methods/string/startswith

Dharman
  • 30,962
  • 25
  • 85
  • 135
JerAnsel
  • 13
  • 4
0

You can make use of the islower() function to check the character at a particular index. s[-1] checks for the last element.

def x_at_either_end(s):
    if len(s) == 0:
        return False
    return s[0].islower() or s[-1].islower()

I would not recommend using startswith and endswith for this.

DollarAkshay
  • 2,063
  • 1
  • 21
  • 39
0

There are ambiguities in the question so this may not be quite what is needed. However:

def x_at_either_end(s):
    return len(s) > 0 and (s[0].islower() or s[-1].islower())
DarkKnight
  • 19,739
  • 3
  • 6
  • 22