我对python很陌生,我正在尝试制作一个刽子手游戏。我现在有一行代码是word = (random.choice(open("Level1py.txt").readline()))
。你知道吗
我得到了错误'str' object does not support item assignment
。你知道吗
这是我剩下的代码(抱歉搞砸了):
import random
def checkLetter(letter, word, guess_word):
for c in word:
if c == letter:
guess_word[word.index(c)] = c
word[word.index(c)] = '*'
print(guess_word)
word = (random.choice(open("Level1py.txt").readline().split()))
guess_word = ['_' for x in word]
print(guess_word)
while '_' in guess_word:
guess = input('Letter: ')
print(checkLetter(guess, word, guess_word))
字符串在python中是不可变的。一个简单的解决方法是使用列表,列表是可变的:
输出
编辑:下面是如何在自己的代码中实现它
请注意,还有其他问题与此无关,如打印
None
和重复打印您还可以使用字典解决问题:
相关问题 更多 >
编程相关推荐