我在涉及字符串的Python问题上有点麻烦。提示是:
修改first_and_last函数,如果字符串的第一个字母与字符串的最后一个字母相同,则返回True,否则返回False。请记住,您可以使用message [0]或message [-1]访问字符。请注意如何处理空字符串,该字符串应返回True,因为什么都不等于什么。
这是我所拥有的:
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?
“索引号0和-1对Python中的字符串处理意味着什么?” -好,这已经给你了。