猿问

将“in list”放入 if 语句中

我目前正在创建一个刽子手游戏。我的问题出在第二个 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()


一只甜甜圈
浏览 145回答 3
3回答

心有法竹

您应该执行以下操作。将 newwordlist 放在 while 循环之外只需替换 for 循环内 newwordlist 的索引即可。请参阅下面的代码。`ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'V', 'W', 'X', 'Y', 'Z']word = input()&nbsp; # word player 2 is trying to guesswordlist = list(word)&nbsp; # word in list formguessedletter = input()&nbsp; # guessed letterguesses = 6&nbsp; # max amount of guesses#define the newwordlist outside while loopnewwordlist = ['-' for letter in word]while guesses <= 6 and guesses > 0:&nbsp; &nbsp; newABC = []&nbsp; &nbsp; newABCstring = ('')&nbsp; &nbsp; for x in ALPHABET:&nbsp; &nbsp; &nbsp; &nbsp; if x != guessedletter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newABC.append(x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newABCstring = (newABCstring + str(x))&nbsp; &nbsp; print("Unused letters:" + " " + (newABCstring))&nbsp; &nbsp; ALPHABET = newABC#&nbsp; &nbsp; &nbsp;newwordlist = []&nbsp; &nbsp; for index, x in enumerate(wordlist):&nbsp; # ['H', 'E', 'L', 'L', 'O']&nbsp; &nbsp; &nbsp; &nbsp; if x in newwordlist or x == guessedletter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #just replace the value in the newwordlist&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newwordlist[index] = x&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #blow elif is not required#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif x not in newwordlist or x != guessedletter:#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newwordlist.append('-')&nbsp; # ['-', '-', 'L', 'L', '-']&nbsp; &nbsp; print(newwordlist)&nbsp; &nbsp; newwordliststring = ('')&nbsp; &nbsp; for x in newwordlist:&nbsp; &nbsp; &nbsp; &nbsp; newwordliststring = (newwordliststring + str(x))&nbsp; &nbsp; &nbsp; &nbsp; if len(newwordliststring) == len(newwordlist):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Guess the word," + " " + (newwordliststring))&nbsp; # prints the guessedletter+dashes in string form&nbsp; &nbsp; guessedletter = input()`

婷婷同学_

您可能想尝试一下并将其与您的进行比较:class E(Exception): passA = []for i in range(65, 91):&nbsp; &nbsp; A.append(chr(i))w = []for i in 'HELLO':&nbsp; &nbsp; w.append(i)g = ['-'] * len(w)d = {}for i in w:&nbsp; &nbsp; d[i] = 0print('Please guess the word :\n%s\n' % g)while True:&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; i = input('Guess letter/word: ').upper()&nbsp; &nbsp; &nbsp; &nbsp; if __debug__:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(i) != 1 and i not in A:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise AssertionError('Only a single letter and ASCII permitted')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else: raise E&nbsp; &nbsp; except AssertionError as e:&nbsp; &nbsp; &nbsp; &nbsp; print('\n%s' % e.args[0])&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; except E:&nbsp; &nbsp; &nbsp; &nbsp; if i in w:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i not in g:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g[w.index(i, d[i])] = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[i] = w.index(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(g)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g[w.index(i, d[i]+1)] = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[i] = w.index(i, d[i]+1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(g)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; if w == g:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('You are great')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break输出:PS D:\Python> python p.pyPlease guess the word :['-', '-', '-', '-', '-']Guess letter/word: h['H', '-', '-', '-', '-']Guess letter/word: e['H', 'E', '-', '-', '-']Guess letter/word: l['H', 'E', 'L', '-', '-']Guess letter/word: l['H', 'E', 'L', 'L', '-']Guess letter/word: lGuess letter/word: lGuess letter/word: o['H', 'E', 'L', 'L', 'O']You are great

噜噜哒

while 循环中的每次迭代都会执行“newwordlist = []”。因此,即使在上一次迭代中设置了“H”,它也会重置为[]。您可以有一个 newwordlist,其元素在 while 循环之外全部初始化为“-”。每次玩家正确猜出字母时,只需更新该列表即可,而不是将元素附加到列表中。newwordlist = ['-' for x in wordlist]while guesses > 0:&nbsp; &nbsp; # some code&nbsp; &nbsp; for idx, letter in enumerate(wordlist):&nbsp; &nbsp; &nbsp; &nbsp; if letter == wordlist[idx]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newwordlist[idx] = letter&nbsp; &nbsp; print(newwordlist)&nbsp; &nbsp; # remaining code&nbsp;&nbsp;
随时随地看视频慕课网APP

相关分类

Python
我要回答