亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將“in list”放入 if 語句中

將“in list”放入 if 語句中

一只甜甜圈 2024-01-04 10:38:05
我目前正在創建一個劊子手游戲。我的問題出在第二個 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 guesswordlist = list(word)  # word in list formguessedletter = input()  # guessed letterguesses = 6  # max amount of guesseswhile 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()
查看完整描述

3 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

您應該執行以下操作。

  1. 將 newwordlist 放在 while 循環之外

  2. 只需替換 for 循環內 newwordlist 的索引即可。請參閱下面的代碼。

`

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


#define the newwordlist outside while loop

newwordlist = ['-' for letter in word]

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 index, x in enumerate(wordlist):  # ['H', 'E', 'L', 'L', 'O']

        if x in newwordlist or x == guessedletter:

            #just replace the value in the newwordlist

            newwordlist[index] = x  

            #blow elif is not required

#         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()

`


查看完整回答
反對 回復 2024-01-04
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

您可能想嘗試一下并將其與您的進行比較:


class E(Exception): pass


A = []


for i in range(65, 91):

    A.append(chr(i))


w = []


for i in 'HELLO':

    w.append(i)


g = ['-'] * len(w)

d = {}


for i in w:

    d[i] = 0


print('Please guess the word :\n%s\n' % g)

while True:

    try:

        i = input('Guess letter/word: ').upper()

        if __debug__:

            if len(i) != 1 and i not in A:

                raise AssertionError('Only a single letter and ASCII permitted')

            else: raise E

    except AssertionError as e:

        print('\n%s' % e.args[0])

        continue


    except E:

        if i in w:

            if i not in g:

                g[w.index(i, d[i])] = i

                d[i] = w.index(i)

                print(g)

            else:

                try:

                    g[w.index(i, d[i]+1)] = i

                    d[i] = w.index(i, d[i]+1)

                    print(g)

                except ValueError:

                    continue


        if w == g:

            print('You are great')

            break

輸出:


PS D:\Python> python p.py

Please 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: l

Guess letter/word: l

Guess letter/word: o

['H', 'E', 'L', 'L', 'O']

You are great


查看完整回答
反對 回復 2024-01-04
?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

while 循環中的每次迭代都會執行“newwordlist = []”。因此,即使在上一次迭代中設置了“H”,它也會重置為[]。


您可以有一個 newwordlist,其元素在 while 循環之外全部初始化為“-”。每次玩家正確猜出字母時,只需更新該列表即可,而不是將元素附加到列表中。


newwordlist = ['-' for x in wordlist]

while guesses > 0:

    # some code

    for idx, letter in enumerate(wordlist):

        if letter == wordlist[idx]:

            newwordlist[idx] = letter

    print(newwordlist)

    # remaining code  


查看完整回答
反對 回復 2024-01-04
  • 3 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號