Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How I can fix "TypeError: list indices must be integers or slices, not str"

I’m working on a kata in codewars called Fat Fingers, here are the instructions:

Freddy has a really fat left pinky finger, and every time Freddy tries
to type an A, he accidentally hits the CapsLock key! Given a string
that Freddy wants to type, emulate the keyboard misses where each A
supposedly pressed is replaced with CapsLock, and return the string
that Freddy actually types. It doesn’t matter if the A in the string
is capitalized or not. When CapsLock is enabled, capitalization is
reversed, but punctuation is not affected.

And here are the examples:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"The quick brown fox jumps over the lazy dog."
-> "The quick brown fox jumps over the lZY DOG."

"The end of the institution, maintenance, and administration of government, is to secure the existence of the body politic, to protect it, and to furnish the individuals who compose it with the power of enjoying in safety and tranquillity their natural rights, and the blessings of life: and whenever these great objects are not obtained, the people have a right to alter the government, and to take measures necessary for their safety, prosperity and happiness."
-> "The end of the institution, mINTENnce, ND dministrTION OF GOVERNMENT, IS TO SECURE THE EXISTENCE OF THE BODY POLITIC, TO PROTECT IT, nd to furnish the individuLS WHO COMPOSE IT WITH THE POWER OF ENJOYING IN Sfety ND TRnquillity their nTURl rights, ND THE BLESSINGS OF LIFE: nd whenever these greT OBJECTS re not obtINED, THE PEOPLE Hve RIGHT TO lter the government, ND TO Tke meSURES NECESSry for their sFETY, PROSPERITY nd hPPINESS."

"aAaaaaAaaaAAaAa"
-> ""

My code:

def fat_fingers(string):
   #create a variable called capitalized ans initial it to false
    #iterate a string and check if the current letter equal to A
    #if its equal to a i will reverse the capitalize var.
    #when i will append the c letter i will check if the capitalized = true
    #then i will append it to a string Uppercased else i will do it Undercased
    caps=False
    stri=""
    string2= list(string)
    for i in string2:
        letter = string2[i]
        if letter == "a" or letter == "A" :
            caps = not caps
        else:
            if caps :
                stri += letter.upper()
            else:
                stri+=letter
        return str(stri)

>Solution :

The for loop for i in string2: iterates over the letters of string2.
You instead are expecting it to give you indices over the string.

Just use i as the letter:

def fat_fingers(string):
   #create a variable called capitalized ans initial it to false
    #iterate a string and check if the current letter equal to A
    #if its equal to a i will reverse the capitalize var.
    #when i will append the c letter i will check if the capitalized = true
    #then i will append it to a string Uppercased else i will do it Undercased
    caps=False
    stri=""
    string2= list(string)
    for letter in string2:
        if letter == "a" or letter == "A" :
            caps = not caps
        else:
            if caps :
                stri += letter.upper()
            else:
                stri+=letter
        return str(stri)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading