Python error TypeError: string indices must be integer

I'm kind of new to Python, so if this is a stupid mistake, please forgive me!

I am working on a password generator to attend the tech club. The way it works is that it asks you to enter a word. The word you enter turns into a list. Then it changes every letter in the list to something else to create a unique password (I know it's messed up). When I run the code, it says TypeError: string indices must be integers

. What's wrong with my code?

print ("Leo Password Generator")
print ('Please enter a word')
word = input()


print ('Enter another word:')
word2 = input()
word = word2 + word
word.split()

def print_list(toon):
    for i in toon:
        if toon[i] == 'e':
            toon[i] = '0'


print_list(word)
print (word)

      

+3


source to share


2 answers


The problem is when you are passing a string to print_list

. When you iterate over a string, it breaks it into single-character lines. So essentially you are making a call toon['a']

that doesn't work because you need to use an integer to access the iterable by index.

Note also that both you and Batukhan are wrong about how you deal with strings. Even if you fix the error above, you will still get another one right after that. In python string

does not allow element assignment, so you will need to create a completely new string rather than reassign one character in it.

If you want, you could probably use a list comprehension to accomplish the same task in a significantly smaller space. Here's an example:



def print_list(toon):
    return ''.join([ch if ch != 'e' else '0' for ch in toon])

      

This creates a new line from toon where all "e" cases are replaced with "0" and all non-e characters remain the same.

Edit: I may have misunderstood your purpose. word.split()

since the whole statement does nothing - split

does not reassign, and you will need to do word = word.split()

if you want the word to equal the list of strings after this statement. But - is there a reason why you are trying to split the string in the first place? And why are you assigning two separate words to the same variable named word

? It doesn't make any sense, and it is very difficult for us to tell what you are trying to accomplish.

+1


source


For loop

already gives you the value of the next available item. In your case is i

not an index , this is the value...

However, if you want to achieve the index and value, you can use enumerate

:

def print_list(toon):
    for i, ch in enumerate(toon):
        if ch == 'e':
            toon = toon[:i] + '0' + toon[i+1:]

    print(toon)

      



or you can iterate over the string in the traditional method:

def print_list(toon):
    for i in range(len(toon)):
        if toon[i] == 'e':
            toon = toon[:i] + '0' + toon[i+1:]

    print(toon)

      

EDIT: As @furkle pointed out since strings

immutable

, they cannot be changed using indices. So use concatenation or method replace

.

0


source







All Articles