Home > Enterprise >  how can i use multiple returned indices in a for loop to update a variable?
how can i use multiple returned indices in a for loop to update a variable?

Time:06-29

My question is: is there any way i can somehow use all the returned capital letter indices and replace them ALL with an underscore? I wished to take the returned values from the uppercase_finder function and insert an underscore in front of those capitalized letters. However, when I run the program, I only get the first capital letter of input with an underscore. Can I somehow iterate all the returned uppercase indices into the part where I insert underscores?

def main():
    first_input = input("input here: ")
    uppercase_indice = uppercase_finder(first_input)
    new_case = first_input[:uppercase_indice]   "_"   first_input[uppercase_indice:]
    new_case = new_case.lower()
    print(new_case)

def uppercase_finder(x):
    for i in range(len(x)):
        if x[i].isupper():
            return i

main()

CodePudding user response:

Okay so based on the assumption that the overall goal is to print out the string inputted all lowercase and an underscore appended to each letter that was uppercase.

You could iterate through each letter in the string without focusing on the indices at all. Something like:

def main():
    first_input = input("input here: ")
    updated_input = ""
    for letter in first_input:
        if(letter.isupper()):
            updated_input  = "_"   letter.lower()
        else:
            updated_input  = letter

    print(updated_input)

Output:

input here: HeLLo
_he_l_lo

Generally though if you want to stick with the uppercase_finder function, the return statement in the loop stops the loop the moment any letter that is uppercase is found. In order to get all of the indices of each letter that is uppercase you would need something like this:

def uppercase_finder(x):
    list_of_indices = []
    for i in range(len(x)):
        if x[i].isupper():
            list_of_indices.append(i)

    return list_of_indices

Then in the main function you can iterate across the list:

for index in uppercase_indice:
   # Make string manipulations for each index

CodePudding user response:

It's obviously an assignment problem so I'm not going to spoon-feed the answer. But I can point out what's the problem in your uppercase_finder.

The problem is that it is returning the index as soon as it find the first upper case. What you can do is

def uppercase_finder(x):
    uppercase_indices = []
    for i in range(len(x)):
        if x[i].isupper():
            # Append the index to the list uppercase_indices
    return uppercase_indices 

CodePudding user response:

There's some problem with your uppercase_finder function, 'return' denotes the end of a function, whenever a return is met, the function will immediately stop and exit with an returned value. For ur case, it seems u wanna return all the indices where there are a capital letter, u may use yield instead of return thus making the function a generator.

def uppercase_finder(x):
    for i in range(len(x)):
        if x[i].isupper():
            yield i

use the output of a generator via a loop:

for capital_pos in uppercase_finder(first_input):
   do_sth
  • Related