我目前正在制作一个刽子手游戏。我的问题在第二个for循环中,对于单词列表中的x。这个for循环应该经过单词列表(在本例中为['H','E','L','L','O']),并且:
if x in newwordlist or x==guessedletter
,然后将x打印到新列表上
elif x not in newwordlist or x!=guessed letter
,然后为该值打印一个“-”
当我在控制台中键入字母H作为猜测字母时。 newwordlist将更新并成为['H'、'-'、'-'、'-'、'-'、'-'],因为我在代码中指定了如果x=guessedletter,则打印
但是,当我继续进行下一个输入时,比如O。newwordlist现在是['-','-','-','-','-','O']。 我如何使它变成['H','-','-','-','-','-','O']
My for循环指定如果x在newwordlist中,则打印它,如果不是,则不打印“-”。为什么不记得newwordlist包含值H,不应该用破折号替换。它应该打印该值
ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z']
word = input() # word player 2 is trying to guess
wordlist = list(word) # word in list form
guessedletter = input() # guessed letter
guesses = 6 # max amount of guesses
while guesses <= 6 and guesses > 0:
newABC = []
newABCstring = ('')
for x in ALPHABET:
if x != guessedletter:
newABC.append(x)
newABCstring = (newABCstring + str(x))
print("Unused letters:" + " " + (newABCstring))
ALPHABET = newABC
newwordlist = []
for x in wordlist: # ['H', 'E', 'L', 'L', 'O']
if x in newwordlist or x == guessedletter:
newwordlist.append(x)
elif x not in newwordlist or x != guessedletter:
newwordlist.append('-') # ['-', '-', 'L', 'L', '-']
print(newwordlist)
newwordliststring = ('')
for x in newwordlist:
newwordliststring = (newwordliststring + str(x))
if len(newwordliststring) == len(newwordlist):
print("Guess the word," + " " + (newwordliststring)) # prints the guessedletter+dashes in string form
guessedletter = input()
您可能希望尝试此方法并将其与您的进行比较:
输出:
您应该执行以下操作
`
`
对于while循环中的每个迭代,都会执行“newwordlist=[]”。因此,即使在上一次迭代中设置了“H”,也会将其重置为[]
在while循环之外,可以有一个newwordlist,其中元素被初始化为“-”。每当玩家猜对字母时,只需更新该列表,而不是在列表中添加元素
相关问题 更多 >
编程相关推荐