将"in list"放入 if 语句中



我目前正在创建一个刽子手游戏。我的问题在第二个for循环中,对于单词表中的x。这个for循环应该经过单词列表(在本例中为['H','E','L','O'](和:

if x in newwordlist or x==guessedletter,则x被打印到新列表上

CCD_ 2-"是为该值打印的。

当我在控制台中键入字母H作为留言时。newwordlist会更新并变为['H','-','-''-','-'],因为我在代码中指定,如果x=guesseddeletter,则打印。

但是,当我继续进行下一个输入时,比如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()

;newwordlist=[]";为while循环中的每个迭代执行。因此,即使在上一次迭代中设置了"H",它也会重置为[]。

您可以在while循环外将元素初始化为"-"的newwordlist。每次玩家正确猜测字母时,只需更新该列表,而不是在列表中添加元素。

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   

您应该执行以下操作。

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

`

您可能想尝试一下并将其与您的进行比较

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%sn' % 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

最新更新