简体   繁体   English

Python错误TypeError:字符串索引必须是整数

[英]Python error TypeError: string indices must be integers

I'm kind of new to Python so if this is a silly mistake, please forgive me! 我对Python很陌生,所以如果这是一个愚蠢的错误,请原谅我!

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 . 当我运行代码时,它说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)

The problem is that you're passing a string to print_list . 问题是你要将一个字符串传递给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. 所以,基本上你正在做的是调用toon['a'] ,这是行不通的,因为你必须使用一个整数来通过索引访问一个iterable。

Note also that both you and Batuhan are making a mistake in the way you're dealing with strings. 另请注意,你和Batuhan都在处理字符串的方式上犯了错误。 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. 在python中, string不允许项目分配,因此您将不得不创建一个全新的字符串,而不是在其中重新分配单个字符。

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. 这将从toon创建一个新的字符串,其中'e'的所有发生率都被替换为'0',并且所有非'''字符都像以前一样被保留。

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. word.split()作为一个语句的整体不做任何事情 - split不会重新分配,你必须做word = word.split()如果你想在之后用word等于字符串列表声明。 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 ? 为什么要为一个名为word变量分配两个单独的word That doesn't make any sense, and makes it very difficult for us to tell what you're trying to accomplish. 这没有任何意义,使我们很难说出你想要完成的事情。

For loop already gives you the value of the next available item. For loop已经为您提供了下一个可用项的 In your case, i is not an index , it is the value itself. 在你的情况下, i不是一个索引 ,它本身就是价值

However, if you want to reach to both index and the value, you can use enumerate : 但是,如果要同时访问索引和值,可以使用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. 编辑:正如@furkle指出的那样,因为stringsimmutable ,所以不能使用索引来更改它们。 So use concatenation, or replace method. 所以使用连接或replace方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 错误 - TypeError:字符串索引必须是整数 - python error - TypeError: string indices must be integers Python错误:“ TypeError:字符串索引必须为整数” - Python Error:“TypeError: string indices must be integers” python和json,错误-TypeError:字符串索引必须为整数 - python and json, Error - TypeError: string indices must be integers 类型错误:使用 Python 解析 Json 时,字符串索引必须是整数错误 - TypeError: string indices must be integers error when parsing Json with Python Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str 如何修复 Python 中的“TypeError:字符串索引必须是整数”错误 - How to fix "TypeError: string indices must be integers" error in Python Python 和 JSON 错误 - TypeError:字符串索引必须是整数 - Python and JSON Error - TypeError: string indices must be integers 如何在Python中修复'TypeError:字符串索引必须是整数'错误 - How to fix 'TypeError: string indices must be integers' error in Python Python Json TypeError:字符串索引必须是整数错误 - Python Json TypeError: string indices must be integers error Python:类型错误:字符串索引必须是 python 中的整数 - Python: TypeError: string indices must be integers in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM