我面临的问题是每当我输入正确的字母时,当我调用存储在我的“getword”变量下的函数时,它不反映列表“lettersGuessed”中的更新。
请参阅“hangman”功能(最后一个)。
<Button x:Name="LeftImport" HorizontalAlignment="Left" Grid.Column="3" Style="{DynamicResource MetroCircleButtonStyle}" Width="30" Height="30" BorderThickness="0">
<Button.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock FontWeight="Bold">Import</TextBlock>
<TextBlock>Import a package</TextBlock>
</StackPanel>
</ToolTip>
</Button.ToolTip>
<Rectangle Width="20" Height="20" Fill="White">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_download}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
答案 0 :(得分:1)
在正确猜测之后,您再也没有再次调用getGuessedWord(secretWord,lettersGuessed)
,您最初只调用一次,然后使用永远不会更新的变量(getword
)。如果您将print "Good guess:" + str(getword)
更改为print "Good guess:" + getGuessedWord(secretWord,lettersGuessed)
,那么您将被设置。无需在str()
的返回值上调用getGuessedWord
,因为它会返回一个字符串。
它应该类似于:
print "You have " + str(chances) + " " + "guesses left"
guess = raw_input("Please enter a letter: ").lower()
if guess in secretWord:
lettersGuessed.append(guess)
print "Good guess:" + getGuessedWord(secretWord,lettersGuessed)
else:
mistakesMade+=1
chances -= 1
print "Oops! That letter is not in my word" + getGuessedWord(secretWord,lettersGuessed)
答案 1 :(得分:0)
def hangman(secretWord):
'''
secretWord: string, the secret word to guess.
Starts up an interactive game of Hangman.
* At the start of the game, let the user know how many
letters the secretWord contains.
* Ask the user to supply one guess (i.e. letter) per round.
* The user should receive feedback immediately after each guess
about whether their guess appears in the computers word.
* After each round, you should also display to the user the
partially guessed word so far, as well as letters that the
user has not yet guessed.
Follows the other limitations detailed in the problem write-up.
'''
# FILL IN YOUR CODE HERE...
print('Welcome to the game, Hangman!')
print('I am thinking of a word that is ', len(secretWord) , ' letters long.')
guesses = 8
lettersGuessed = list()
availableletter = getAvailableLetters(lettersGuessed)
print('-------------')
while guesses > 0:
print('You have ', guesses,' guesses left')
print('Available letters: ', availableletter)
guess = input("Please guess a letter: ").lower()
if guess in lettersGuessed:
print("Oops! You've already guessed that letter: ", getGuessedWord(secretWord, lettersGuessed))
else:
lettersGuessed.append(guess)
availableletter = getAvailableLetters(lettersGuessed)
if guess in secretWord:
print('Good guess: ', getGuessedWord(secretWord, lettersGuessed))
else:
print('Oops! That letter is not in my word: ', getGuessedWord(secretWord, lettersGuessed))
guesses -=1
print ("------------")
if isWordGuessed(secretWord, lettersGuessed):
print('Congratulations, you won!')
break
if guesses == 0:
print('Sorry, you ran out of guesses. The word was', secretWord)