FlyFox6767
FlyFox6767

Reputation: 3

what's wrong with this code? It's seems to be ok but my school auto feedback says it can't run it

def last_early(check_str):

    x = check_str.count(check_str[-1])

    if( x > 1):
        valid = True
    else:
        valid = False
    return valid

my_str = input("enter str: ")
my_str = my_str.lower()

valid = last_early(my_str)
print(valid)

the question is: "The function accepts as a string parameter. The function returns "true" if the character that appears last in the string also appears earlier. Otherwise, false will be printed"

Upvotes: 0

Views: 76

Answers (1)

wjandrea
wjandrea

Reputation: 33107

when check_str is empty string i.e. '', you would get IndexError. Maybe you are failing this edge test-case.

-- comment by Ch3steR

To see if this is the problem, try adding a guard clause for the empty string:

def last_early(check_str):
    if not check_str:
        return False

    ...

Upvotes: 1

Related Questions