I am in a intro to GIS programming class and doing a hangman game for my final project. I am very much an amateur so this might be an easy fix that I am not understanding...Here is my code:
import random, sys class Hangman: def init(self,word): self.word = word self.incorrect_letters = [] self.letters_guessed = []
def hangman_loss(self):
retu self.hangman_win() or (len(self.incorrect_letters) == 7)
def hangman_win(self):
if '_' not in self.unknown_word():
retu True
retu False
def game_stats(self):
print graphic[len(self.incorrect_letters)]
print 'Guess the word: ' + self.unknown_word()
print 'Incorrect Letters: ',
for letter in self.incorrect_letters:
print letter,
print
def unknown_word(self):
rtn = ''
for letter in self.word:
if letter not in self.letters_guessed:
rtn += '_'
else:
rtn += letter
retu rtn
def guess(self,letter):
if letter in self.word and letter not in self.letters_guessed:
self.letters_guessed.append(letter)
elif letter not in self.word and letter not in self.incorrect_letters:
self.incorrect_letters.append(letter)
else:
retu False
retu True
graphic = [ ' +---+ n | | n | n | n | n | n========= n', ' +---+ n | | n 0 | n | n | n | n========= n', ' +---+ n | | n 0 | n | | n | n | n========= n', ' +---+ n | | n 0 | n | | n | n | n========= n', ' +---+ n | | n 0 | n /| | n | n | n========= n', ' +---+ n | | n 0 | n /| | n | n | n========= n', ' +---+ n | | n 0 | n /| | n / | n | n========= n', ' +---+ n | | n 0 | n /| | n / | n | n========= n' ]
def rand_word():
fruit = ['grape','apple','pear','banana','orange', 'cherry']
retu fruit[random.randint(0,len(fruit))]
def main(): print 'nWelcome to Hangman!nYou are allowed 7 incorrect guesses before you lose the game. nThe theme is fruit. Good luck!n' game = Hangman(rand_word()) while not game.hangman_loss(): game.game_stats() player_choice = raw_input('nEnter a letter: ') if len(player_choice)>= 2: while True: print 'nPlease only one letter at a time' retry = raw_input('Try again: ') if len(retry) == 1: game.guess(retry) break else: continue else: game.guess(player_choice)
game.game_stats()
if game.hangman_win():
print 'nGreat job! You have won Hangman!'
else:
print 'nSorry, but you did not win the game!'
print 'The word was ' + game.word
print 'Good try!'
print
answer = raw_input('Would you like to play again? (yes or no): ')
print
if answer == 'yes':
main()
else:
print
print ('Thanks for playing!')
sys.exit()
main()
Sometimes when I run it, it throws the error:
Traceback (most recent call last): File "C:/Users/Jackie Carbert/Documents/GEOG 4880/hangmanTINKERing.py", line 135, in main() File "C:/Users/Jackie Carbert/Documents/GEOG 4880/hangmanTINKERing.py", line 126, in main main() File "C:/Users/Jackie Carbert/Documents/GEOG 4880/hangmanTINKERing.py", line 126, in main main() File "C:/Users/Jackie Carbert/Documents/GEOG 4880/hangmanTINKERing.py", line 88, in main game = Hangman(rand_word()) File "C:/Users/Jackie Carbert/Documents/GEOG 4880/hangmanTINKERing.py", line 82, in rand_word retu fruit[random.randint(0,len(fruit))] IndexError: list index out of range
But only sometimes. Obviously I see that the error is referring to the function where I am picking one of the random fruits, but I do not see what, why or how the index is out of range?
Sorry for the messy code, couldn't get it all to be in one block..
