我目前正在研究python hangman程序。我有一些基础知识,例如显示空白和检测正确的字母,但是我无法弄清楚何时正确识别了单词或句子。
EDIT
阅读评论后,我更新了代码,使用snake_case而不是camelCase命名了变量,并将输出添加到了show_word函数中。
def show_word(word, known_letters):
word = word.lower()
characters = ""
for i in word:
if i in known_letters:
characters = characters + i + " "
elif i == " ":
characters = characters + "/ "
else:
characters = characters + "_ "
print characters
return characters.replace(" ", "").replace("/", " ")
def guess_word(word, known_letters):
all_letters = "abcdefghijklmnopqrstuvwxyz"
word = word.lower()
guesses = 0
while show_word(word, known_letters) != word:
print("Guess a letter")
guess = raw_input()
if guess in known_letters:
print("You have already guessed %s" % (guess))
elif len(guess) != 1:
print("Only type one letter")
else:
known_letters += guess
guesses += 1
print("Well done you have guessed the word(s) (%s) in %s guesses" % (word, guesses))
guess_word("The quick brown fox jumps over the lazy dog", "")
该程序现在可以正确识别何时我猜对了所有正确的字母。感谢所有提供帮助的人。如果其他人在此问题上需要帮助,则下面是我的原始代码。
def showWord(word, knownLetters):
word = word.lower()
characters = ""
for i in word:
if i in knownLetters:
characters = characters + i + " "
elif i == " ":
characters = characters + "/ "
else:
characters = characters + "_ "
print characters
def guessWord(word, knownLetters):
word = word.lower()
guesses = 0
while showWord(word, knownLetters) != word:
print("Guess a letter")
guess = raw_input()
if guess in knownLetters:
print("You have already guessed %s" % (guess))
elif len(guess) != 1:
print("Only guess one letter")
else:
knownLetters += guess
guesses += 1
print("Well done, you guessed the word (%s) in %s guesses" % (word, guesses))
showWord(word, knownLetters)
guessWord("The quick brown fox jumps over the lazy dog", "")
答案 0 :(得分:0)
您的showWord()
函数默认不会返回None
,因为它不会显式返回任何内容。
您可能应该在return characters.replace(' ', '').replace('/', ' ')
之后添加print characters
,并且应该满足您的最终条件。
由于字母不会与showWord()
完全对齐,因此您需要调整word
的输出。
当showWord()
实际上返回您的字符串时,以下陈述将正确地等同于
while showWord(word, knownLetters) != word:
然后将工作,因为可以将characters
与word
进行比较。
此外,Python的约定是在snake_case中而不是camelCase中命名变量。
最后,要重定向您的奖金问题,很多人都使用Python制作了ASCII Hangman,也许您可以参考,学习和采用这些方法。如果您对此有任何疑问,可以在Stack Overflow上询问另一个问题,以明确显示您尝试过的内容。
答案 1 :(得分:0)
这是一些工作代码的模板,您可以希望使用它们来找出修改代码的方法。
def guessWord(word, knownLetters):
word = word.lower()
cur_word = ['_'] * len(word)
result_to_compare = ""
num_guesses = 0
while result_to_compare != word:
print("Guess a letter!")
guess = raw_input()
if guess in knownLetters:
print("You have already guessed this letter!")
else:
knownLetters += guess
for i in range(len(word)):
if guess is word[i]:
cur_word[i] = guess
print(cur_word)
result_to_compare = ''.join(cur_word)
print("Congrats! You guessed it!")
guessWord("severe", "")
我使用cur_word作为临时变量来显示用户的进度。经过有效猜测后,我从cur_word中的值创建字符串result_to_compare并将其与实际解决方案进行比较。希望这可以帮助。只要用户输入raw_input作为空格,这将在单词之间使用空格。您应该能够从这里想出如何对其进行修改以按自己的喜好工作。
答案 2 :(得分:0)
这是我的版本,我使代码尽可能冗长,因此应该易于说明。
def showWord(word,knownLetters):
word=word.lower()
characters=""
wordsofar=""
for char in word:
if char in knownLetters:
characters = characters + char + " "
wordsofar += char
elif char == " ":
characters = characters + "/ "
wordsofar += " "
else:
characters += "_ "
print characters
return validate(word, wordsofar)
def validate(word, knownLetters):
word_dict = {}
for c in word:
if c not in word_dict:
word_dict[c] = 1
else:
word_dict[c] += 1
for c in knownLetters:
if c not in word_dict:
return False
else:
word_dict[c] -=1
for k,v in word_dict.iteritems():
if v != 0:
return False
return True
然后在while循环中,您将检查showWord(word,knownLetters)是否返回false。