mou*_*aim 1 python buffer python-3.x
我收到错误:
类型错误:类型 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)
Run Code Online (Sandbox Code Playgroud)
导致错误的行是:
if(guess in secretWord ) :
Run Code Online (Sandbox Code Playgroud)
这条线有什么问题?
如果你想在自己的机器上运行代码,这里有一个指向 words.txt 的链接
通过在打开文件时使用模式,您将单词加载为二进制数据'rb':
inFile = open(WORDLIST_FILENAME, 'rb', 0)
Run Code Online (Sandbox Code Playgroud)
然后尝试查看该二进制数据中是否包含字符串:
if(guess in secretWord ) :
Run Code Online (Sandbox Code Playgroud)
如果WORDLIST_FILENAME是文本,请不要使用二进制模式来读取它。使用文本模式:
inFile = open(WORDLIST_FILENAME, 'r', encoding='ascii')
Run Code Online (Sandbox Code Playgroud)
您链接的文件是一个简单的 ASCII 编码文件,因此我使用该编解码器将其解码为 Unicode 字符串。