1

I'm having a bit of trouble with a Python problem that involves strings.

The prompt is:

Modify the first_and_last function so that it returns True if the first letter of the string is the same as the last letter of the string, False if they’re different. Remember that you can access characters using message[0] or message[-1]. Be careful how you handle the empty string, which should return True since nothing is equal to nothing.

This is what I have:

def first_and_last(message):
  for char in message:
    if char[0] == char[-1]:
      return True
    elif char == " ":
      return True
    else:
      return False

print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

And the output I'm receiving:

True
True
None

Not quite, first_and_last("tree") returned True, should be False. Have you added the check for empty strings, and used correct string indexing? Hint: what do the index numbers of 0 and -1 mean for string handling in Python?

Anyone have any idea how to help?

martineau
  • 119,623
  • 25
  • 170
  • 301
cesaire talom
  • 11
  • 1
  • 1
  • 2
  • Hint: When indexing strings, the index numbers `0` and `-1` mean mean the first character and last character respectively of the string. – martineau Mar 22 '20 at 00:04

7 Answers7

1

I'll avoid answering the problem itself and try to give some hints to guide you:

  • "What do the index numbers of 0 and -1 mean for string handling in Python?" - well, this was already given to you.
  • What is the difference between char and message? What types of objects are both of those? In fact, char[0] and char[-1] seems odd. Shouldn't that cause an exception?
  • Programming languages, especially ones like python, are a playground. Printing objects usually helps you understand what's going on. (Try printing each char in the message and see what you get)
dtc
  • 1,774
  • 2
  • 21
  • 44
1

You can use the 'not' and 'or' comparison operators to output the desired result.

def first_and_last(message):
    if not message or message[0] == message[-1]:
        return True
    else:
        return False
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
minTwin
  • 1,181
  • 2
  • 21
  • 35
0
def first_and_last(message):
  if not message or message[0] == message[len(message)-1]:
    return True
  else:
    return False
4b0
  • 21,981
  • 30
  • 95
  • 142
  • Please add some explanation to your answers on stackoverflow – zoran404 Apr 30 '20 at 22:21
  • While this code may provide a solution to problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution. – E. Zeytinci May 01 '20 at 12:01
0
def first_and_last(message):
    if len(message)==0:
        return True
    else:
        return message[0]==message[-1]
slava-kohut
  • 4,203
  • 1
  • 7
  • 24
0
def first_and_last(message):
  if not message or message[0] == message[len(message)-1]:
    return True
  else:
    return False

print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

Here is your output:
True
False
True
Harvey
  • 9
  • 2
0
def first_and_last(message):
    if len(message)==0 or message[-1]==message[0]:
        return True
    else:
        return False

print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

Here is your output:

True
False
True
S.B
  • 13,077
  • 10
  • 22
  • 49
0

Hey I was getting the same issue with the problem when I was trying to solve it. Mainly I was getting issue for the null string. If you do not use or operation in your code try using the condition for null string first.

Anu
  • 13
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32236366) – Rabinzel Jul 17 '22 at 06:15