ハングマンゲームを構成する2つの関数を記述する必要があります。私はコードを持っていますが、エラーが発生し続け、コードが完全に実行されないため、コードが正しいかどうかわかりません。これは私が持っているコードです。ここでは何が起こっているハングマンゲームの出力エラー
Welcome to the Hangman game!
I am thinking of a word that is 1 letters!
You have 8 guesses left
Available letters: abcdefghijklmnopqrstuvwxyz
Please guess a letter: c
Traceback (most recent call last):
File "submission.py", line 81, in hangman
if isWordGuessed(secretWord, lettersGuessed) == "True":
File "simple_grade_hangman.py", line 107, in isWordGuessed
if letter not in lettersGuessed:
TypeError: argument of type 'NoneType' is not iterable
:
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
'''
if lettersGuessed == []:
return False
else:
for i in secretWord:
if i not in lettersGuessed:
return False
else:
if i == secretWord[-1]:
return True
def getGuessedWord(secretWord, lettersGuessed):
lst= ''
for e in secretWord:
if e in lettersGuessed:
lst += e
else:
lst += '_'
return lst
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.
'''
Alletters = string.ascii_lowercase
result = list(Alletters)
for i in lettersGuessed:
if i in result:
result.remove(i)
transtring = ''.join(result)
return transtring
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.
'''
print("Welcome to the Hangman game!")
print("I am thinking of a word that is " + str(len(secretWord)) + " letters!")
guesses = 8
lettersGuessed = []
Alletters = string.ascii_lowercase
while guesses > 0:
print("You have " + str(guesses) + " guesses left")
print("Available letters: " + str(Alletters))
letters = input("Please guess a letter: ")
if type(letters) != str:
print("Invalid input! please enter one letter!")
else:
letterslower = letters.lower()
lettersGuessed = lettersGuessed.append(letterslower)
if letterslower not in Alletters:
print("Opps! you have already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed))
else:
if isWordGuessed(secretWord, lettersGuessed) == "True":
print("Congradualations, you won!")
else:
print("Good guess: " + getGuessedWord(secretWord, lettersGuessed))
guesses -= 1
Alletters = getAvailableLetters(lettersGuessed)
print("You have ran out of guess, the word is " + str(secretWord))
これは出力(エラーは黄色である)でしょうか?
ブール値と文字列 "True"を比較する理由は何ですか? – Carcigenicate
isWordGuessed(secretWord、lettersGuessed)== "True"の場合、 'isWordGuessed(secretWord、lettersGuessed):'の場合のみ 'に変更します。 1.真実と決して比較しない。それは不必要に冗長です。あなたはブール値そのものではなく、文字列と比較していました。なぜこれが間違っているのかを強調するために、 '' False''はブール値のコンテキスト(空でない文字列と同様)で 'True'と評価されます。 – Carcigenicate
@Carcigenicate説明をありがとう - 意味があります。 – EllaP