3

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

I have been working on a Password Generator to present at tech club. How it works is that it asks for you to type in a word. The word you enter is turned into a list. Then it changes each letter in the list to something else to make a unique password (it's flawed, I know). When I run the code, it says TypeError: string indices must be integers. What is wrong with my code?

print ("Leo's 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)
1
  • 1
    I've edited your post a little bit to clarify what your intended behavior is, based on your comments on furkle's answer. Please let me know if I've misrepresented you at all! Commented Nov 3, 2014 at 23:58

2 Answers 2

1

The problem is that you're passing a string to print_list. When you iterate through a string, it splits it into single-character strings. So, essentially what you're doing is calling toon['a'], which doesn't work, because you have to use an integer to access an iterable by index.

Note also that both you and Batuhan are making a mistake in the way you're dealing with strings. Even once you fix the error above, you're still going to get another one immediately afterwards. In python, string doesn't allow item assignment, so you're going to have to create an entirely new string rather than reassigning a single character therein.

If you wanted, you could probably use a list comprehension to accomplish the same task in significantly less 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 string from toon where all incidences of 'e' have been replaced with '0', and all non-'e' characters are left as before.

Edit: I might have misunderstood your purpose. word.split() as the entirety of a statement doesn't do anything - split doesn't reassign, and you'd have to do word = word.split() if you wanted to word to equal a list of strings after that statement. But - is there a reason you're trying to split the string in the first place? And why are you assigning two separate words to a single variable called word? That doesn't make any sense, and makes it very difficult for us to tell what you're trying to accomplish.

Sign up to request clarification or add additional context in comments.

11 Comments

@LeoLinden You could do that if you wanted, or like Batuhan said you could use enumerate to get both the character at any given index and the integer of that index. But if you look at the second paragraph, that's not going to fix your method, because you can't reassign characters by index in strings in python.
You don't need the inner throwaway list in your join: you can just use a generator expression in there. ''.join(ch if ch...)
Okay... Now I'm getting an EOL during scanning error
@LeoLinden I think I might have slightly misunderstood what you're trying to accomplish here. Did you intend for word to be a list of strings when you pass it to print_list? Because split() doesn't reassign in place, you'd have to do word = word.split().
@Leo if you have another question, ask another question. The comments of an answer are not the correct place to debug unrelated issues.
|
0

For loop already gives you the value of the next available item. In your case, i is not an index, it is the value itself.

However, if you want to reach to both index and the 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 a 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 are immutable, they cannot be changed using indexes. So use concatenation, or replace method.

8 Comments

This won't work at all - you can't reassign the index of a string in python.
Cool. I wasn't the one who downvoted, so I can't reverse it, but note that the user may have been unclear about what they were intending to do. According to them, they may have wanted to pass a list rather than a string.
That didn't cause any errors, but it didn't change the e's to 0's
Why don't you just use, toon.replace('e', '0')
@LeoLinden: This does create a new string with every e replaced by 0… but then it just throws that new string away. It's exactly the same problem you had with split. You need to return toon at the end of the function, and you need to call it with word = print_list(word). (Also, you really should rename the function; it's misleading to call something print_list when doesn't take a list and doesn't print anything…)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.