我目前正在创建一个刽子手游戏。我的问题出在第二个 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', '-', '-', '-', '-'] 因为我在代码中指定 if x=guessedletter then print。
但是,当我继续进行下一个输入时,例如 O。新词列表现在是 ['-', '-', '-', '-', 'O']。我该如何使它变成 ['H', '-', '-', '-', 'O']。
我的 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()
心有法竹
婷婷同学_
噜噜哒
相关分类