Closed. This question needs debugging details 。它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 Stack Overflow 的 on-topic

2年前关闭。



Improve this question




我是 python 的初学者,我正在尝试制作一个刽子手游戏,但我似乎无法让它工作。
这是我的代码:
word = "street"
letters = list(word)
dashes = ["_","_","_","_","_","_"]
guess = input("Guess the letter. ") #assuming that "e" was the input
x = [index for index, value in enumerate(letters) if value == guess]
dashes[x] = guess

我想替换 dashes 中具有 x 索引的破折号。在 "e" 是输入的情况下,这意味着 dashes[3]dashes[4] 变成 "e"dashes[x] = guess 似乎不起作用。

最佳答案

list comprehension :

print([v if i not in x else guess for i,v in enumerate(dashes)])

关于python - 如何用两个索引替换列表中的项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53295150/

10-12 16:34