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
What does the choose_word(wordlist) return?
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)
Apparently what random.choice returns is not something you can use within len() try printing out the "word" variable before passing it to len()
Join our real-time social learning platform and learn together with your friends!