Home > Mobile >  How to continue a condition statement after the first one is meet in python?
How to continue a condition statement after the first one is meet in python?

Time:09-30

I'm creating a program that transfers camel case words to snake words, for every capital case in the string(pretend the first letter is always lower case), it switches to a lower case and adds a "_" before it. I've been trying many ways and it only checks the first capital letter and then returns.

def main():
    camels = input("camelCase: ")
    print(snake(camels))


def snake(camels):
    while True:
        for camel in camels:
            if camel.isupper():
                a = camels.replace(camel, "_"   camel.lower())
                return a
                continue
            else:
                pass


if __name__ == "__main__":
    main()

For example, if the input is...

camelCase: helloPythonWorld

expected output: hello_python_world

output now: hello_pythonWorld

CodePudding user response:

It is because of the return. Other problem is that if the first letter is uppercase the snake_case word will start with "_"

def snake(camels):
    a = camels
    i = 1
    for camel in camels[1:]:
        if (camel.isupper()):
            a = a[:i]   "_"   camel.lower()   a[i 1:]
            i  = 1
        i  = 1
    return a.lower()
  • Related