[英]Python - Hangman Game with multiple letters in one word
I am new to python and have a problem with a hangman game that I was making. 我是python的新手,正在制作的子手游戏有问题。 So I have a list called "current" which is filled with hyphens (-) the same amount of times as the length of the word the person is guessing.
因此,我有一个名为“当前”的列表,其中填充了连字符(-),该次数与该人正在猜测的单词的长度相同。 When they guess a letter correctly it replaces the hyphen with the letter in the correct place.
当他们正确猜出一个字母时,它会将连字符替换为正确位置的字母。 The problem I have is that if there are 2 or more of the same letter in a word then it works for the first time the letter comes up but after that it will say the letter has already been guessed therefore it doesn't work and I cant figure out how to fix this.
我的问题是,如果一个单词中有两个或两个以上相同的字母,则该字母首次出现会起作用,但之后它会说该字母已经被猜到,因此它不起作用,我无法弄清楚如何解决此问题。
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!")
Thanks 谢谢
By using the string.find-method, you only get the first occurrence of the character in the word. 通过使用string.find-method,您只会在单词中首次出现该字符。 By defining this method, you get all occurrences:
通过定义此方法,可以得到所有出现的事件:
def find_all(word, guess):
return [i for i, letter in enumerate(word) if letter == guess]
You should also add a "continue" after checking if you've already guessed the letter before, so that you don't add it to the list again. 您还应该在检查之前是否已经猜过字母之后再添加“继续”,以免再次将其添加到列表中。
This should work: 这应该工作:
def find_all(word, guess):
return [i for i, letter in enumerate(word) if letter == guess]
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.")
continue
guessed.append(guess)
if guess in theword:
indices = find_all(theword, guess)
x = list(current)
for i in indices:
x[i] = guess
current = "".join(x)
print ("Correct! \nYour guesses: %s" % (guessed))
print(x)
else:
print ("Incorrect, try again")
lives = lives -1
Instead of: 代替:
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)
Try this: 尝试这个:
for index,character in enumerate(theword):
x = list(current)
if character == guess:
x[index] = guess
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.