简体   繁体   中英

function should return a list of all the indexes in the string that have capital letters

Tried a different approach to what I have seen. Im going through each line of code however I'm not seeing where the issue is with this code

def capital_indexes(s):
    cap_list = []            
    for idx, letter in enumerate(s):
        if letter.isupper() == True:
            cap_list.append(idx)
            return cap_list

You return after first iteration: It should be:

def capital_indexes(s):
    cap_list = []            
    for idx, letter in enumerate(s):
        if letter.isupper():
            cap_list.append(idx)
    
    return cap_list

And as a oddity, you can (and should!) uselist comprehesion to make code more concise.

def capital_indexes(s):
    return [idx for idx, letter in enumerate(s) if letter.isupper()]

the Indentation of return is not right, the return statement is inside the if statement, so after finding first capital it would exit: the right place is outside the for loop:

def capital_indexes(s):
    cap_list = []
    for idx, letter in enumerate(s):
        if letter.isupper():
            cap_list.append(idx)
    return cap_list

Ok thank you guys, I have added the corrections but I still don't seem to be getting the index of the capital letters, is it working for you guys?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM