

def camelcase_to_underscore(text):
    """
    Convert a camelCasedString to one separated_by_underscores. Treats
    neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
    not become u_r_l. That would be stupid.

    :param text: The string to convert.
    """
    outstr = []

    for char in text:
        if char.is_lower():
            outstr.append(char)
        elif outstr[-1].is_lower():
            outstr.append('_')
            outstr.append(char.lower())
        else:
            outstr.append(char.lower())

    return ''.join(outstr)



def camelcase_to_underscore(text):
    """
    Convert a camelCasedString to one separated_by_underscores. Treats
    neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
    not become u_r_l. That would be stupid.

    :param text: The string to convert.
    """
    outstr = []

    for char in text:
        if char.islower():
            outstr.append(char)
        elif (len(outstr) > 0) and (outstr[-1].islower()):
            outstr.append('_')
            outstr.append(char.lower())
        else:
            outstr.append(char.lower())

    return ''.join(outstr)

Correct an AttributeError and a potential IndexErr
Correct an AttributeError and a potential IndexErr

  
  
  def camelcase_to_underscore(text):
      """
      Convert a camelCasedString to one separated_by_underscores. Treats
      neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
      not become u_r_l. That would be stupid.
  
      :param text: The string to convert.
      """
      outstr = []
  
      for char in text:
-         if char.is_lower():
+         if char.islower():
              outstr.append(char)
-         elif outstr[-1].is_lower():
+         elif (len(outstr) > 0) and (outstr[-1].islower()):
              outstr.append('_')
              outstr.append(char.lower())
          else:
              outstr.append(char.lower())
  
      return ''.join(outstr)
  
Correct an AttributeError and a potential IndexErr
## Code Before:


def camelcase_to_underscore(text):
    """
    Convert a camelCasedString to one separated_by_underscores. Treats
    neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
    not become u_r_l. That would be stupid.

    :param text: The string to convert.
    """
    outstr = []

    for char in text:
        if char.is_lower():
            outstr.append(char)
        elif outstr[-1].is_lower():
            outstr.append('_')
            outstr.append(char.lower())
        else:
            outstr.append(char.lower())

    return ''.join(outstr)

## Instruction:
Correct an AttributeError and a potential IndexErr
## Code After:


def camelcase_to_underscore(text):
    """
    Convert a camelCasedString to one separated_by_underscores. Treats
    neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
    not become u_r_l. That would be stupid.

    :param text: The string to convert.
    """
    outstr = []

    for char in text:
        if char.islower():
            outstr.append(char)
        elif (len(outstr) > 0) and (outstr[-1].islower()):
            outstr.append('_')
            outstr.append(char.lower())
        else:
            outstr.append(char.lower())

    return ''.join(outstr)
