我收到了错误:
TypeError:类型str不支持缓冲区API
尝试运行以下代码时:
import random
import string
WORDLIST_FILENAME = "words.txt"
def loadWords():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print ("Loading word list from file...")
# inFile: file
inFile = open(WORDLIST_FILENAME, 'rb', 0)
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = line.split()
print (" ", len(wordlist), "words loaded.")
return wordlist
def chooseWord(wordlist):
"""
wordlist (list): list of words (strings)
Returns a word from wordlist at random
"""
return random.choice(wordlist)
# end of helper code
# -----------------------------------
# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = loadWords()
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
'''
for x in secretWord :
if x not in lettersGuessed :
return False
return True
def getGuessedWord(secretWord, lettersGuessed):
'''
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.
'''
guessedWord=['_']*len(secretWord)
for x in lettersGuessed :
## 1** find the occurences of x in secretWord
occurences = []
for i, j in enumerate(secretWord):
if j == x:
occurences.append(i)
## 2* go to the same indices in guessedWord and replace them by x
for i in occurences :
guessedWord[i]=x;
return ''.join(guessedWord)
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.
'''
available_Letters=[]
all_letters=list(string.ascii_lowercase)
for x in all_letters :
if (x not in lettersGuessed):
available_Letters.append(x)
return ''.join(available_Letters)
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 game, Hangman!")
print("I am thinking of a word that is 4 letters long.")
number_of_guesses = 8
lettersGuessed = []
while True :
print("--------------------------------")
print ("You have " , number_of_guesses , "left !")
print("Available letters : " , getAvailableLetters(lettersGuessed) )
print("Please guess a letter : ")
guess = input()
while True:
print("Please guess a letter : ")
if guess in lettersGuessed:
print("Oops ! you've already guessed that letter :" , getGuessedWord(secretWord, lettersGuessed))
break
else :
if(guess in secretWord ) :
lettersGuessed.append(guess)
print("Good guess:", getGuessedWord(secretWord, lettersGuessed) )
else :
number_of_guesses-=1
print("---------------------------------")
if(number_of_guesses == 0) :
print(" Sorry, you ran out of guesses. The word was else. ")
break
if(isWordGuessed(secretWord, lettersGuessed)):
print(" Congratulations, you won!")
break
secretWord = chooseWord(wordlist).lower()
hangman(secretWord)
导致错误的行是:
if(guess in secretWord ) :
这条线有什么问题?
如果您想在自己的计算机上运行代码,here是words.txt的链接
答案 0 :(得分:4)
您在打开文件时使用'rb'
模式将您的文字加载为二进制数据:
inFile = open(WORDLIST_FILENAME, 'rb', 0)
然后尝试查看该二进制数据中是否包含字符串:
if(guess in secretWord ) :
如果WORDLIST_FILENAME
是文本,请不要使用二进制模式来读取它。使用文本模式:
inFile = open(WORDLIST_FILENAME, 'r', encoding='ascii')
您链接的文件是一个简单的ASCII编码文件,因此我使用该编解码器将其解码为Unicode字符串。