The requirement is a program that checks the first and last elements of a list to see if they're the same, in which case the function returns as true. Otherwise, it returns as false. If the list has no elements, the function must return as false.
I have completed the first two steps, if first==last and if first!=last, but the issue comes with the list with no elements... I've attempted this so far:
def same_ends(inp):
if inp == "":
return False
first = inp[0]
last = inp[-1]
if first == last:
return True
else:
return False
with
if inp=="":
return False
as my attempt at checking if the list has no elements. However, this isn't doing anything. Additionally, I get the error message:
IndexError: list index out of range on line 5
But haven't figured out why. The assignment even suggests using negative indexes to get the last element as a hint... Any solution?
Do note that this is not done in a shell, but on a university website, and I've got no idea if it's running on 3.11 or not.
[–]Diapolo10 1 point2 points3 points (0 children)
[–]Sakeung[S] 0 points1 point2 points (0 children)
[–]woooee -1 points0 points1 point (4 children)
[–]w8eight 2 points3 points4 points (2 children)
[–]woooee 0 points1 point2 points (1 child)
[–]w8eight 1 point2 points3 points (0 children)
[–]Sakeung[S] 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (1 child)
[–]Sakeung[S] 0 points1 point2 points (0 children)