def isWordGuessed(secretWord, lettersGuessed): ''' secretWord: string, the word the user is guessing lettersGuessed: list, what letters have been guessed so far returns: boolean, True if all the letters of secretWord are in lettersGuessed; False otherwise ''' gussed = True for elm in secretWord: if elm not in lettersGuessed: gussed = False return gussed
getGuessedWord(secretWord, lettersGuessed)
1 2 3 4 5 6 7 8 9 10 11 12
def getGuessedWord(secretWord, ): ''' secretWord: string, the word the user is guessing lettersGuessed: list, what letters have been guessed so far returns: string, comprised of letters and underscores that represents what letters in secretWord have been guessed so far. ''' ans = list(secretWord) for i in range(0,len(secretWord)): if ans[i] not in lettersGuessed: ans[i] = ' _ ' return ''.join(ans)
getAvailableLetters(lettersGuessed)
1 2 3 4 5 6 7 8 9 10 11 12
def getAvailableLetters(lettersGuessed): ''' lettersGuessed: list, what letters have been guessed so far returns: string, comprised of letters that represents what letters have not yet been guessed. ''' alpha = list(string.ascii_lowercase) ans =[] for i in range(0,len(alpha)): if alpha[i] not in lettersGuessed: ans.append(alpha[i]) return ''.join(ans)
* 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. ''' num_word = len(secretWord) print("Welcome to the game, Hangman!") print("I am thinking of a word that is " + str(num_word) + " letters long.") print("-----------") letters_Guessed = [] left_guess = 8 while left_guess > 0: print("You have " + str(left_guess) + " guesses left.") print("Available letters: " + str(getAvailableLetters(letters_Guessed))) # get input guess from player: # Please guess a letter: a new_input_letter = input("Please guess a letter: ") # Case one : if You have alreaday guessed while new_input_letter in letters_Guessed: print("Oops! You've already guessed that letter: " + str(getGuessedWord(secretWord, letters_Guessed))) print("-----------") print("You have " + str(left_guess) + " guesses left.") print("Available letters: " + str(getAvailableLetters(letters_Guessed))) new_input_letter = input("Please guess a letter: ") letters_Guessed.append(new_input_letter) # Case Two: success guess if new_input_letter in secretWord: print("Good guess: " + str(getGuessedWord(secretWord, letters_Guessed))) print("-----------") else : print("Oops! That letter is not in my word: " + str(getGuessedWord(secretWord, letters_Guessed))) print("-----------") left_guess -= 1 # check the game win condition if isWordGuessed(secretWord, letters_Guessed): print("Congratulations, you won!") return None print("Sorry, you ran out of guesses. The word was else.") return None
Welcome to the game, Hangman! I am thinking of a word that is 1 letters long. ----------- You have 8 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: c Good guess: c ----------- Congratulations, you won! None
2:单词中是重复字母的情况
1 2 3 4 5 6 7 8 9 10
Welcome to the game, Hangman! I am thinking of a word that is 3 letters long. ----------- You have 8 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: z Good guess: zzz ----------- Congratulations, you won! None
Welcome to the game, Hangman! I am thinking of a word that is 1 letters long. ----------- You have 8 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: a Oops! That letter is not in my word: _ ----------- You have 7 guesses left. Available letters: bcdefghijklmnopqrstuvwxyz Please guess a letter: b Oops! That letter is not in my word: _ ----------- You have 6 guesses left. Available letters: cdefghijklmnopqrstuvwxyz Please guess a letter: d Oops! That letter is not in my word: _ ----------- You have 5 guesses left. Available letters: cefghijklmnopqrstuvwxyz Please guess a letter: e Oops! That letter is not in my word: _ ----------- You have 4 guesses left. Available letters: cfghijklmnopqrstuvwxyz Please guess a letter: f Oops! That letter is not in my word: _ ----------- You have 3 guesses left. Available letters: cghijklmnopqrstuvwxyz Please guess a letter: g Oops! That letter is not in my word: _ ----------- You have 2 guesses left. Available letters: chijklmnopqrstuvwxyz Please guess a letter: h Oops! That letter is not in my word: _ ----------- You have 1 guesses left. Available letters: cijklmnopqrstuvwxyz Please guess a letter: i Oops! That letter is not in my word: _ ----------- Sorry, you ran out of guesses. The word was else. None
Welcome to the game, Hangman! I am thinking of a word that is 3 letters long. ----------- You have 8 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: a Good guess: _ _ a ----------- You have 8 guesses left. Available letters: bcdefghijklmnopqrstuvwxyz Please guess a letter: e Good guess: _ ea ----------- You have 8 guesses left. Available letters: bcdfghijklmnopqrstuvwxyz Please guess a letter: a Oops! You've already guessed that letter: _ ea ----------- You have 8 guesses left. Available letters: bcdfghijklmnopqrstuvwxyz Please guess a letter: e Oops! You've already guessed that letter: _ ea ----------- You have 8 guesses left. Available letters: bcdfghijklmnopqrstuvwxyz Please guess a letter: s Good guess: sea ----------- Congratulations, you won! None
Welcome to the game, Hangman! I am thinking of a word that is 1 letters long. ----------- You have 8 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: x Oops! That letter is not in my word: _ ----------- You have 7 guesses left. Available letters: abcdefghijklmnopqrstuvwyz Please guess a letter: z Oops! That letter is not in my word: _ ----------- You have 6 guesses left. Available letters: abcdefghijklmnopqrstuvwy Please guess a letter: x Oops! You've already guessed that letter: _ ----------- You have 6 guesses left. Available letters: abcdefghijklmnopqrstuvwy Please guess a letter: z Oops! You've already guessed that letter: _ ----------- You have 6 guesses left. Available letters: abcdefghijklmnopqrstuvwy Please guess a letter: y Good guess: y ----------- Congratulations, you won! None