![]() |
I'm getting a String index out of range error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I'm getting a String index out of range error (/thread-37564.html) |
I'm getting a String index out of range error - debian77 - Jun-26-2022 This function returns True if the first letter of the string is the same as the last letter of the string and False if they’re different. I'm having trouble explaining why I have the following error with this code: def first_and_last(message): if message[0] == message[-1]: return True return False print(first_and_last("else")) print(first_and_last("tree")) print(first_and_last("")) But if I just add a logical operator it doesn't have an error, my question is why? if both are basically the same, I think the same error should pop out because I'm not doing anything special to specify any input for the second one.def first_and_last(message): if message == "" or message[0] == message[-1]: return True return False print(first_and_last("else")) print(first_and_last("tree")) print(first_and_last("")) Thanks for reading.
RE: I'm getting a String index out of range error - rob101 - Jun-26-2022 Very strange. Your code (the first code block) works fine for me (Linux OS running Python 3.6.9). The only thing I'd alter (not that it changes the operation on my system), is to have an 'else' clause in the 'if' branch. def first_and_last(message): if message[0] == message[-1]: return True else: return FalseI'm old school and as such I don't like to see an 'if' branch without an 'else' clause. RE: I'm getting a String index out of range error - Larz60+ - Jun-26-2022 >>> message = 'a' >>> message[0] 'a' >>> message[-1] 'a' >>> message = "" >>> message[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> message[-1] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range if message == "" or message[0] == message[-1]: in this example, the message == "" would test True, so conditional would be satisfied, and is done at that point.
RE: I'm getting a String index out of range error - menator01 - Jun-26-2022 One example def f_and_l(msg): if msg[0] == msg[-1:]: return True return False print(f_and_l('else')) print(f_and_l('tree'))
RE: I'm getting a String index out of range error - deanhystad - Jun-26-2022 Why use True or False when the if statement evaluates to True or False? I can see no reason you should get an index error for the provided test cases. I think the test for an empty string is a good improvement, but it is not needed for "tree" or "else". Cannot explain how you got the error, but it is not from the code you posted. RE: I'm getting a String index out of range error - debian77 - Jun-26-2022 Thank you, everyone. It was my fault, there was a final print function with an "" parameter at the end. print(first_and_last(""))So, I think that was the error. RE: I'm getting a String index out of range error - rob101 - Jun-26-2022 (Jun-26-2022, 03:53 AM)debian77 Wrote: Thank you, everyone. It was my fault, there was a final print function with an "" parameter at the end. Easy done. To mitigate the error, you could test for an empty string at the beginning of your function... def first_and_last(message): if message: if message[0] == message[-1]: return True else: return False else: return False... which will return 'False' if an empty string is passed to the function. RE: I'm getting a String index out of range error - deanhystad - Jun-26-2022 If and else need not apply. def first_and_last(message): return message == "" or message[0] == message[-1] |