【发布时间】:2014-03-01 12:42:19
【问题描述】:
我是 python 新手,并且对我正在制作的刽子手游戏有疑问。所以我有一个名为“当前”的列表,其中包含连字符 (-) 的次数与人们猜测的单词的长度相同。当他们正确猜出一个字母时,它会将连字符替换为正确位置的字母。我遇到的问题是,如果一个单词中有 2 个或更多相同的字母,那么它在第一次出现该字母时有效,但之后它会说该字母已经被猜到,因此它不起作用,我无法弄清楚如何解决此问题。
current = "_" * len(theword)
x = list(current)
print (x)
guessed = []
while current != theword and lives > 0:
print ("You have %d lives left" % lives)
guess = input("Please input one letter or type 'exit' to quit.")
guess = guess.lower()
if guess == "exit":
break
elif guess in guessed:
print ("You have already guessed this letter, please try again.")
guessed.append(guess)
if guess in theword:
index = theword.find(guess)
x = list(current)
x[index] = guess
current = "".join(x)
print ("Correct! \nYour guesses: %s" % (guessed))
print(x)
else:
print ("Incorrect, try again")
lives = lives -1
if current == theword:
print ("Well done, you have won!")
谢谢
【问题讨论】:
标签: python