python - Hangman Python 相同字符的两个实例
问题描述
elif guess in word:
already_guessed.extend([guess])
index = word.find(guess)
word = word[:index] + "_" + word [index +1:]
display = display[:index] + guess + display[index +1:]
print (display + "\n")
要猜的词是女王,如果我猜 e 它不会填满 e 的两个实例。你会如何解决这个问题?
解决方案
扩展zvone的答案:
elif guess in word:
already_guessed.extend([guess])
for index in range(len(word)):
if word[index] == guess:
word = word[:index] + "_" + word [index +1:]
display = display[:index] + guess + display[index +1:]
print (display + "\n")
推荐阅读
- r - 如何强制所有数据在同一行结束?
- google-apps-script - 如何通过 google api 更改 GSuite 的用户默认语言?
- node.js - 如何在不使用互联网的情况下从纱线缓存安装软件包?
- r - 在R中删除部分字符串
- sql - 是否可以使用附加行扩展选择,以便每个组具有相同的行数?
- vapor - 与 Vapor 4/Fluent 的交易
- arrays - postgres中jsonb数组内的非空约束
- arrays - 如何使用>>处理嵌套数组并返回一个平面数组?
- charts - 如何向 Chart.js 甜甜圈图显示第二组标签?
- excel - 如何从传递的变量类型不同的函数返回值