所以我有这个清单: visual = [' _',' _',' _'] 并且用户输入一个随机字母来查找结束词(游戏是刽子手)我有办法将其打印出来但它看起来像这样:
_
0
_
我希望它看起来像这样: _ o _ 要么: [' _',' o',' _'] 我不介意哪个人更喜欢第一个。
到目前为止,我有这个:
visuals = ['_ ', '_ ', '_ ']
userInput = input("Enter a letter to guess: ")
index = 0
for ch in hangmanWord:
index += 1
if ch == userInput:
visuals = [w.replace([index], userInput) for w in visuals]
print(visuals)
但是最后一行似乎不起作用(我认为它是替换线)它只是出现了这个:
['_ ', '_ ', '_ ']
我希望下划线被用户输入替换。
抱歉它的长度。非常感谢帮助。
答案 0 :(得分:0)
将index += 1
移至if
语句后,以使替换从0开始。
对于替换者,只需使用visual[index] = userInput
。它将根据索引编辑visual
。
答案 1 :(得分:0)
此脚本解决了问题
>>> hangmanWord = "hello"
>>> visuals = ['_'] *len(hangmanWord)
>>> ended = False
>>> while not ended:
... userInput = raw_input("Enter a letter to guess: ")
... for idx,ch in enumerate(hangmanWord):
... if userInput == hangmanWord[idx]:
... visuals[idx] = userInput
... print visuals
... ended = '_' not in visuals
...
Enter a letter to guess: a
['_', '_', '_', '_', '_']
Enter a letter to guess: h
['h', '_', '_', '_', '_']
Enter a letter to guess: e
['h', 'e', '_', '_', '_']
Enter a letter to guess: l
['h', 'e', 'l', 'l', '_']
Enter a letter to guess: o
['h', 'e', 'l', 'l', 'o']