Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将索引转换为整数?_Python - Fatal编程技术网

Python 如何将索引转换为整数?

Python 如何将索引转换为整数?,python,Python,我设法找到字符“t”的索引的第一个打印结果。我试图使用结果索引插入到猜测位置,这样我就可以替换字符所在的星号。但在这种情况下,我收到了错误 word = "python" store = [] guess = ['*'] * len(word) for index, char in enumerate(word): if char == 't': store.append(index) print("the index of i is: ", store)

我设法找到字符“t”的索引的第一个打印结果。我试图使用结果索引插入到猜测位置,这样我就可以替换字符所在的星号。但在这种情况下,我收到了错误

word = "python"
store = []
guess = ['*'] * len(word)
for index, char in enumerate(word):
    if char == 't':
        store.append(index)
        print("the index of i is: ", store)

        print(char)

        guess[store] = char
        print(''.join(guess))

你需要这样的东西:

word = "python"
store = []
guess = ['*'] * len(word)
for index, char in enumerate(word):
    if char == 't':
        store.append(index)
        print("the index of '%s' is: %s"%(char, index))

        print(char)

        guess[index] = char #guess is list and index is integer here
        print(''.join(guess))

变量
store
是一个列表,而不是索引。你试过使用实际名为
index
的变量吗?我上周才开始学习python。index和store之间有什么不同?没有给出相同的数字吗?存储是列表,例如[2],索引是整数,例如2,您可以使用
索引
存储[0]