您的 def answer_form(answer): 返回一个连接列表 - 使其成为一个字符串。您不能修改字符串,它们是不可变的(参见 f.e. Immutable vs Mutable types)。但是,您可以重新创建它们,例如通过切片或将它们用作字符列表。请参阅Change one character in a string?。
如果你使用sets,你可以简化你的刽子手。对于尚未猜到的任何字符,打印'_' 更容易,然后操作字符串(并且您也避免操作列表;o):
data = ["hang","man","carrot","supercalifragilisticexpialidocious"]
# random.choice solver (I am lazy ;o) ) - set to False for human player
autosolve = True
import random
from string import ascii_lowercase
aslow = set(ascii_lowercase)
# what should the user guess? random choice from data
what = random.choice(data).lower()
# store the length and characters used in what instead of calculating each time
lwhat = len(what)
swhat = set(what)
# these letters where already guessed
guessed = set()
# amount of tries reached, we quit when twice the amount of characters got tried
tries = 0
while True and tries < 2*lwhat:
# use list comprehension to print _ if a char was not guessed yet, else print char
print("The secret word is: ", ' '.join(x if x in guessed else "_" for x in what))
# we are done if all characters of what are guessed
if swhat.issubset(guessed):
print(f"You did it! It took you {tries} tries")
break
if not autosolve:
guess = input("Guess one letter or the complete word:").lower()
else:
guess = random.choice(list(aslow))
print("Guess one letter or the complete word:", guess)
aslow.remove(guess)
lguess = len(guess)
# only count 1-character and full word guesses
if lguess not in (1,lwhat):
print("1 letter or the while word, try again")
continue
tries += 1
# one character was guessed
if lguess == 1:
guessed.add(guess) # add to set and print some feedback
if guess in what:
print("Correct.")
else:
print("beeeeeep - that was wrong.")
# whole word was guessed
elif guess == what:
print(f"You did it! It took you {tries} tries")
break
else:
print("Too many tries: try again later")
print("The end")
获胜条件是:你在所有猜测的字符中寻找is a subset的单词集合。
输出:
The secret word is: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Guess one letter or the complete word: s
Correct.
The secret word is: s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: n
beeeeeep - that was wrong.
The secret word is: s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: b
beeeeeep - that was wrong.
The secret word is: s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: v
beeeeeep - that was wrong.
The secret word is: s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: j
beeeeeep - that was wrong.
The secret word is: s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: e
Correct.
The secret word is: s _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: q
beeeeeep - that was wrong.
The secret word is: s _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: g
Correct.
The secret word is: s _ _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: x
Correct.
The secret word is: s _ _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: w
beeeeeep - that was wrong.
The secret word is: s _ _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: u
Correct.
The secret word is: s u _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ _ _ _ _ u s
Guess one letter or the complete word: o
Correct.
The secret word is: s u _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ o _ _ o u s
Guess one letter or the complete word: t
Correct.
The secret word is: s u _ e _ _ _ _ _ _ _ _ g _ _ _ s t _ _ e x _ _ _ _ _ _ o _ _ o u s
Guess one letter or the complete word: c
Correct.
The secret word is: s u _ e _ c _ _ _ _ _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: m
beeeeeep - that was wrong.
The secret word is: s u _ e _ c _ _ _ _ _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: h
beeeeeep - that was wrong.
The secret word is: s u _ e _ c _ _ _ _ _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: f
Correct.
The secret word is: s u _ e _ c _ _ _ f _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: i
Correct.
The secret word is: s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i _ o c i o u s
Guess one letter or the complete word: k
beeeeeep - that was wrong.
The secret word is: s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i _ o c i o u s
Guess one letter or the complete word: d
Correct.
The secret word is: s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i d o c i o u s
Guess one letter or the complete word: y
beeeeeep - that was wrong.
The secret word is: s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i d o c i o u s
Guess one letter or the complete word: a
Correct.
The secret word is: s u _ e _ c a _ i f _ a g i _ i s t i c e x _ i a _ i d o c i o u s
Guess one letter or the complete word: r
Correct.
The secret word is: s u _ e r c a _ i f r a g i _ i s t i c e x _ i a _ i d o c i o u s
Guess one letter or the complete word: p
Correct.
The secret word is: s u p e r c a _ i f r a g i _ i s t i c e x p i a _ i d o c i o u s
Guess one letter or the complete word: l
Correct.
The secret word is: s u p e r c a l i f r a g i l i s t i c e x p i a l i d o c i o u s
You did it! It took you 25 tries
The end
不过,“AI”对于简短的词来说真的很糟糕:
The secret word is: _ _ _ _ _ _
Guess one letter or the complete word: q
beeeeeep - that was wrong.
The secret word is: _ _ _ _ _ _
Guess one letter or the complete word: v
beeeeeep - that was wrong.
The secret word is: _ _ _ _ _ _
Guess one letter or the complete word: p
beeeeeep - that was wrong.
The secret word is: _ _ _ _ _ _
Guess one letter or the complete word: o
Correct.
The secret word is: _ _ _ _ o _
Guess one letter or the complete word: y
beeeeeep - that was wrong.
The secret word is: _ _ _ _ o _
Guess one letter or the complete word: a
Correct.
The secret word is: _ a _ _ o _
Guess one letter or the complete word: d
beeeeeep - that was wrong.
The secret word is: _ a _ _ o _
Guess one letter or the complete word: w
beeeeeep - that was wrong.
The secret word is: _ a _ _ o _
Guess one letter or the complete word: m
beeeeeep - that was wrong.
The secret word is: _ a _ _ o _
Guess one letter or the complete word: i
beeeeeep - that was wrong.
The secret word is: _ a _ _ o _
Guess one letter or the complete word: t
Correct.
The secret word is: _ a _ _ o t
Guess one letter or the complete word: j
beeeeeep - that was wrong.
Too many tries: try again later
The end