Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

getting this error on the hangman problem when I use this wordlist = load_words() word = choose_word(wordlist) remLetters = string.lowercase remGuesses = 8 #starting number of guesses #makes a blank with the length of the word blankWord = '' for i in range(0,len(word)): blankWord = blankWord + '_' I get this error:Traceback (most recent call last): File "/Users/Toly/Downloads/ps2 6/ps2_hangman.py", line 55, in for i in range(0,len(word)): TypeError: object of type 'instancemethod' has no len()

OpenStudy (anonymous):

What does the choose_word(wordlist) return?

OpenStudy (anonymous):

look at my earlier code before the block I posted: import random import string WORDLIST_FILENAME = "words.txt" def load_words(): """ 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, 'r', 0) # line: string line = inFile.readline() # wordlist: list of strings wordlist = string.split(line) print " ", len(wordlist), "words loaded." return wordlist def choose_word(wordlist): """ wordlist (list): list of words (strings) Returns a word from wordlist at random """ return random.choice(wordlist)

OpenStudy (anonymous):

Apparently what random.choice returns is not something you can use within len() try printing out the "word" variable before passing it to len()

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!