Hangman Game IndexError: list index out of range





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Im trying to follow a tutorial to write a simple "Hangman style game", It runs ok, if you guess the letter correctly however, on your 1st incorrect guess the programme displays the 2nd ASCII graphic and not the 1st. when you guess your 2nd incorrect letter the 3rd graphic comes up but it is "distorted" like this:



Sorry, z isn't what we're looking for.



        +---------+
| |
| O
| -|-
| / |
================


finally you make a 3rd incorrect guess and I get the error:



Traceback (most recent call last):
File "./HangMan.py", line 118, in <module>
start()
File "./HangMan.py", line 44, in start
while game():
File "./HangMan.py", line 79, in game
hangedman(letters_wrong)
File "./HangMan.py", line 39, in hangedman
print graphic[hangman]
IndexError: list index out of range



  • any advice would be appreciated.


  • Sorry theres no comments on my code I'm VERY new to this..



Heres my code:



from random import *


player_score = 0

computer_score = 0

def hangedman(hangman):
graphic = [
"""
+---------+
|
|
|
|
|
================
""",
"""
+---------+
| |
| O
|
|
|
================
""",
"""
+---------+
| |
| O
| -|-
| /
|
================
"""]

print graphic[hangman]
return

def start():
print "Lets play a game of Hangman"
while game():
pass
scores()

def game():
dictionary = ["box","ted","sad","bad"]
word = choice(dictionary)
word_length = len(word)
clue = word_length * ["_"]
tries = 3
letters_tried = ""
guesses = 0
letters_right = 0
letters_wrong = 0
global computer_score, player_score

while (letters_wrong != tries) and ("".join(clue) != word):
letter = guess_letter()
if len(letter)==1 and letter.isalpha():
if letters_tried.find(letter) !=-1:
print "you've already picked", letter
else:
letters_tried = letters_tried = letter
first_index = word.find(letter)
if first_index == -1:
letters_wrong +=1
print "Sorry,",letter,"isn't what we're looking for."
else:
print "Congratulations,",letter,"is correct."
for i in range(word_length):
if letter == word[i]:
clue[i] = letter
else:
print "Choose another"

hangedman(letters_wrong)
print " ".join(clue)
print "Guesses: ", letters_tried

if letters_wrong == tries:
print "Game Over"
print "The word was",word
computer_score += 1
break
if "".join(clue) == word:
print "you win!"
print "the word was", word
player_score += 1
break
return play_again()


def guess_letter():
print
letter = raw_input("Take a Guess at our mystery word:")
letter.strip()
letter.lower()
print
return letter

def play_again():
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("y"):
return answer
else:
print "Thank you see you next time"

def scores():
global player_score, computer_score
print "Hight Scores"
print "Player: ", player_score
print "Computer: ", computer_score

if __name__ == '__main__':
start()









share|improve this question


















  • 1





    Lists are 0-based. So your list graphic has three entries with indexes 0, 1 and 2. Now you try to access graphic[3] and ... BOOM!

    – Matthias
    Nov 22 '18 at 9:42











  • @Matthias Hi thanks for your comment, are you saying if I change the "tries = 3" to "tries = 2" it should work? If so I have tried that and I just get two guesses and it says game over...

    – PyRMc
    Nov 22 '18 at 9:46













  • I didn't check all of your code (which still has some logical errors like letter.lower() where you don't use the return value). I think you need a fourth image in the list.

    – Matthias
    Nov 22 '18 at 10:11











  • I think the solution would be to have four elements in graphic. Let's add another element where the guy's head, neck and body is shown without the legs.

    – Lajos Arpad
    Nov 22 '18 at 10:24











  • @Lajos Arpad Thanks for your help! adding a 4th graphic helped. now if you guess incorrectly on your 1st try it comes up with the 2nd graphic in the list and if you guess correctly on your 1st guess it displays the 1st graphic? I have made one Syntax error correction, in the while loop the 1st else condition used to read "letters_tried = letters_tried = letter" and now reads "letters_tried = letters_tried + letter". This has had no effect on the game as far as I can tell.... I think the problem is that an incorrect 1st guess calls the graphic indexed 1 and not 0....any thoughts?

    – PyRMc
    Nov 23 '18 at 7:22


















0















Im trying to follow a tutorial to write a simple "Hangman style game", It runs ok, if you guess the letter correctly however, on your 1st incorrect guess the programme displays the 2nd ASCII graphic and not the 1st. when you guess your 2nd incorrect letter the 3rd graphic comes up but it is "distorted" like this:



Sorry, z isn't what we're looking for.



        +---------+
| |
| O
| -|-
| / |
================


finally you make a 3rd incorrect guess and I get the error:



Traceback (most recent call last):
File "./HangMan.py", line 118, in <module>
start()
File "./HangMan.py", line 44, in start
while game():
File "./HangMan.py", line 79, in game
hangedman(letters_wrong)
File "./HangMan.py", line 39, in hangedman
print graphic[hangman]
IndexError: list index out of range



  • any advice would be appreciated.


  • Sorry theres no comments on my code I'm VERY new to this..



Heres my code:



from random import *


player_score = 0

computer_score = 0

def hangedman(hangman):
graphic = [
"""
+---------+
|
|
|
|
|
================
""",
"""
+---------+
| |
| O
|
|
|
================
""",
"""
+---------+
| |
| O
| -|-
| /
|
================
"""]

print graphic[hangman]
return

def start():
print "Lets play a game of Hangman"
while game():
pass
scores()

def game():
dictionary = ["box","ted","sad","bad"]
word = choice(dictionary)
word_length = len(word)
clue = word_length * ["_"]
tries = 3
letters_tried = ""
guesses = 0
letters_right = 0
letters_wrong = 0
global computer_score, player_score

while (letters_wrong != tries) and ("".join(clue) != word):
letter = guess_letter()
if len(letter)==1 and letter.isalpha():
if letters_tried.find(letter) !=-1:
print "you've already picked", letter
else:
letters_tried = letters_tried = letter
first_index = word.find(letter)
if first_index == -1:
letters_wrong +=1
print "Sorry,",letter,"isn't what we're looking for."
else:
print "Congratulations,",letter,"is correct."
for i in range(word_length):
if letter == word[i]:
clue[i] = letter
else:
print "Choose another"

hangedman(letters_wrong)
print " ".join(clue)
print "Guesses: ", letters_tried

if letters_wrong == tries:
print "Game Over"
print "The word was",word
computer_score += 1
break
if "".join(clue) == word:
print "you win!"
print "the word was", word
player_score += 1
break
return play_again()


def guess_letter():
print
letter = raw_input("Take a Guess at our mystery word:")
letter.strip()
letter.lower()
print
return letter

def play_again():
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("y"):
return answer
else:
print "Thank you see you next time"

def scores():
global player_score, computer_score
print "Hight Scores"
print "Player: ", player_score
print "Computer: ", computer_score

if __name__ == '__main__':
start()









share|improve this question


















  • 1





    Lists are 0-based. So your list graphic has three entries with indexes 0, 1 and 2. Now you try to access graphic[3] and ... BOOM!

    – Matthias
    Nov 22 '18 at 9:42











  • @Matthias Hi thanks for your comment, are you saying if I change the "tries = 3" to "tries = 2" it should work? If so I have tried that and I just get two guesses and it says game over...

    – PyRMc
    Nov 22 '18 at 9:46













  • I didn't check all of your code (which still has some logical errors like letter.lower() where you don't use the return value). I think you need a fourth image in the list.

    – Matthias
    Nov 22 '18 at 10:11











  • I think the solution would be to have four elements in graphic. Let's add another element where the guy's head, neck and body is shown without the legs.

    – Lajos Arpad
    Nov 22 '18 at 10:24











  • @Lajos Arpad Thanks for your help! adding a 4th graphic helped. now if you guess incorrectly on your 1st try it comes up with the 2nd graphic in the list and if you guess correctly on your 1st guess it displays the 1st graphic? I have made one Syntax error correction, in the while loop the 1st else condition used to read "letters_tried = letters_tried = letter" and now reads "letters_tried = letters_tried + letter". This has had no effect on the game as far as I can tell.... I think the problem is that an incorrect 1st guess calls the graphic indexed 1 and not 0....any thoughts?

    – PyRMc
    Nov 23 '18 at 7:22














0












0








0








Im trying to follow a tutorial to write a simple "Hangman style game", It runs ok, if you guess the letter correctly however, on your 1st incorrect guess the programme displays the 2nd ASCII graphic and not the 1st. when you guess your 2nd incorrect letter the 3rd graphic comes up but it is "distorted" like this:



Sorry, z isn't what we're looking for.



        +---------+
| |
| O
| -|-
| / |
================


finally you make a 3rd incorrect guess and I get the error:



Traceback (most recent call last):
File "./HangMan.py", line 118, in <module>
start()
File "./HangMan.py", line 44, in start
while game():
File "./HangMan.py", line 79, in game
hangedman(letters_wrong)
File "./HangMan.py", line 39, in hangedman
print graphic[hangman]
IndexError: list index out of range



  • any advice would be appreciated.


  • Sorry theres no comments on my code I'm VERY new to this..



Heres my code:



from random import *


player_score = 0

computer_score = 0

def hangedman(hangman):
graphic = [
"""
+---------+
|
|
|
|
|
================
""",
"""
+---------+
| |
| O
|
|
|
================
""",
"""
+---------+
| |
| O
| -|-
| /
|
================
"""]

print graphic[hangman]
return

def start():
print "Lets play a game of Hangman"
while game():
pass
scores()

def game():
dictionary = ["box","ted","sad","bad"]
word = choice(dictionary)
word_length = len(word)
clue = word_length * ["_"]
tries = 3
letters_tried = ""
guesses = 0
letters_right = 0
letters_wrong = 0
global computer_score, player_score

while (letters_wrong != tries) and ("".join(clue) != word):
letter = guess_letter()
if len(letter)==1 and letter.isalpha():
if letters_tried.find(letter) !=-1:
print "you've already picked", letter
else:
letters_tried = letters_tried = letter
first_index = word.find(letter)
if first_index == -1:
letters_wrong +=1
print "Sorry,",letter,"isn't what we're looking for."
else:
print "Congratulations,",letter,"is correct."
for i in range(word_length):
if letter == word[i]:
clue[i] = letter
else:
print "Choose another"

hangedman(letters_wrong)
print " ".join(clue)
print "Guesses: ", letters_tried

if letters_wrong == tries:
print "Game Over"
print "The word was",word
computer_score += 1
break
if "".join(clue) == word:
print "you win!"
print "the word was", word
player_score += 1
break
return play_again()


def guess_letter():
print
letter = raw_input("Take a Guess at our mystery word:")
letter.strip()
letter.lower()
print
return letter

def play_again():
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("y"):
return answer
else:
print "Thank you see you next time"

def scores():
global player_score, computer_score
print "Hight Scores"
print "Player: ", player_score
print "Computer: ", computer_score

if __name__ == '__main__':
start()









share|improve this question














Im trying to follow a tutorial to write a simple "Hangman style game", It runs ok, if you guess the letter correctly however, on your 1st incorrect guess the programme displays the 2nd ASCII graphic and not the 1st. when you guess your 2nd incorrect letter the 3rd graphic comes up but it is "distorted" like this:



Sorry, z isn't what we're looking for.



        +---------+
| |
| O
| -|-
| / |
================


finally you make a 3rd incorrect guess and I get the error:



Traceback (most recent call last):
File "./HangMan.py", line 118, in <module>
start()
File "./HangMan.py", line 44, in start
while game():
File "./HangMan.py", line 79, in game
hangedman(letters_wrong)
File "./HangMan.py", line 39, in hangedman
print graphic[hangman]
IndexError: list index out of range



  • any advice would be appreciated.


  • Sorry theres no comments on my code I'm VERY new to this..



Heres my code:



from random import *


player_score = 0

computer_score = 0

def hangedman(hangman):
graphic = [
"""
+---------+
|
|
|
|
|
================
""",
"""
+---------+
| |
| O
|
|
|
================
""",
"""
+---------+
| |
| O
| -|-
| /
|
================
"""]

print graphic[hangman]
return

def start():
print "Lets play a game of Hangman"
while game():
pass
scores()

def game():
dictionary = ["box","ted","sad","bad"]
word = choice(dictionary)
word_length = len(word)
clue = word_length * ["_"]
tries = 3
letters_tried = ""
guesses = 0
letters_right = 0
letters_wrong = 0
global computer_score, player_score

while (letters_wrong != tries) and ("".join(clue) != word):
letter = guess_letter()
if len(letter)==1 and letter.isalpha():
if letters_tried.find(letter) !=-1:
print "you've already picked", letter
else:
letters_tried = letters_tried = letter
first_index = word.find(letter)
if first_index == -1:
letters_wrong +=1
print "Sorry,",letter,"isn't what we're looking for."
else:
print "Congratulations,",letter,"is correct."
for i in range(word_length):
if letter == word[i]:
clue[i] = letter
else:
print "Choose another"

hangedman(letters_wrong)
print " ".join(clue)
print "Guesses: ", letters_tried

if letters_wrong == tries:
print "Game Over"
print "The word was",word
computer_score += 1
break
if "".join(clue) == word:
print "you win!"
print "the word was", word
player_score += 1
break
return play_again()


def guess_letter():
print
letter = raw_input("Take a Guess at our mystery word:")
letter.strip()
letter.lower()
print
return letter

def play_again():
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("y"):
return answer
else:
print "Thank you see you next time"

def scores():
global player_score, computer_score
print "Hight Scores"
print "Player: ", player_score
print "Computer: ", computer_score

if __name__ == '__main__':
start()






python ascii python-2.x ascii-art






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 9:39









PyRMcPyRMc

11




11








  • 1





    Lists are 0-based. So your list graphic has three entries with indexes 0, 1 and 2. Now you try to access graphic[3] and ... BOOM!

    – Matthias
    Nov 22 '18 at 9:42











  • @Matthias Hi thanks for your comment, are you saying if I change the "tries = 3" to "tries = 2" it should work? If so I have tried that and I just get two guesses and it says game over...

    – PyRMc
    Nov 22 '18 at 9:46













  • I didn't check all of your code (which still has some logical errors like letter.lower() where you don't use the return value). I think you need a fourth image in the list.

    – Matthias
    Nov 22 '18 at 10:11











  • I think the solution would be to have four elements in graphic. Let's add another element where the guy's head, neck and body is shown without the legs.

    – Lajos Arpad
    Nov 22 '18 at 10:24











  • @Lajos Arpad Thanks for your help! adding a 4th graphic helped. now if you guess incorrectly on your 1st try it comes up with the 2nd graphic in the list and if you guess correctly on your 1st guess it displays the 1st graphic? I have made one Syntax error correction, in the while loop the 1st else condition used to read "letters_tried = letters_tried = letter" and now reads "letters_tried = letters_tried + letter". This has had no effect on the game as far as I can tell.... I think the problem is that an incorrect 1st guess calls the graphic indexed 1 and not 0....any thoughts?

    – PyRMc
    Nov 23 '18 at 7:22














  • 1





    Lists are 0-based. So your list graphic has three entries with indexes 0, 1 and 2. Now you try to access graphic[3] and ... BOOM!

    – Matthias
    Nov 22 '18 at 9:42











  • @Matthias Hi thanks for your comment, are you saying if I change the "tries = 3" to "tries = 2" it should work? If so I have tried that and I just get two guesses and it says game over...

    – PyRMc
    Nov 22 '18 at 9:46













  • I didn't check all of your code (which still has some logical errors like letter.lower() where you don't use the return value). I think you need a fourth image in the list.

    – Matthias
    Nov 22 '18 at 10:11











  • I think the solution would be to have four elements in graphic. Let's add another element where the guy's head, neck and body is shown without the legs.

    – Lajos Arpad
    Nov 22 '18 at 10:24











  • @Lajos Arpad Thanks for your help! adding a 4th graphic helped. now if you guess incorrectly on your 1st try it comes up with the 2nd graphic in the list and if you guess correctly on your 1st guess it displays the 1st graphic? I have made one Syntax error correction, in the while loop the 1st else condition used to read "letters_tried = letters_tried = letter" and now reads "letters_tried = letters_tried + letter". This has had no effect on the game as far as I can tell.... I think the problem is that an incorrect 1st guess calls the graphic indexed 1 and not 0....any thoughts?

    – PyRMc
    Nov 23 '18 at 7:22








1




1





Lists are 0-based. So your list graphic has three entries with indexes 0, 1 and 2. Now you try to access graphic[3] and ... BOOM!

– Matthias
Nov 22 '18 at 9:42





Lists are 0-based. So your list graphic has three entries with indexes 0, 1 and 2. Now you try to access graphic[3] and ... BOOM!

– Matthias
Nov 22 '18 at 9:42













@Matthias Hi thanks for your comment, are you saying if I change the "tries = 3" to "tries = 2" it should work? If so I have tried that and I just get two guesses and it says game over...

– PyRMc
Nov 22 '18 at 9:46







@Matthias Hi thanks for your comment, are you saying if I change the "tries = 3" to "tries = 2" it should work? If so I have tried that and I just get two guesses and it says game over...

– PyRMc
Nov 22 '18 at 9:46















I didn't check all of your code (which still has some logical errors like letter.lower() where you don't use the return value). I think you need a fourth image in the list.

– Matthias
Nov 22 '18 at 10:11





I didn't check all of your code (which still has some logical errors like letter.lower() where you don't use the return value). I think you need a fourth image in the list.

– Matthias
Nov 22 '18 at 10:11













I think the solution would be to have four elements in graphic. Let's add another element where the guy's head, neck and body is shown without the legs.

– Lajos Arpad
Nov 22 '18 at 10:24





I think the solution would be to have four elements in graphic. Let's add another element where the guy's head, neck and body is shown without the legs.

– Lajos Arpad
Nov 22 '18 at 10:24













@Lajos Arpad Thanks for your help! adding a 4th graphic helped. now if you guess incorrectly on your 1st try it comes up with the 2nd graphic in the list and if you guess correctly on your 1st guess it displays the 1st graphic? I have made one Syntax error correction, in the while loop the 1st else condition used to read "letters_tried = letters_tried = letter" and now reads "letters_tried = letters_tried + letter". This has had no effect on the game as far as I can tell.... I think the problem is that an incorrect 1st guess calls the graphic indexed 1 and not 0....any thoughts?

– PyRMc
Nov 23 '18 at 7:22





@Lajos Arpad Thanks for your help! adding a 4th graphic helped. now if you guess incorrectly on your 1st try it comes up with the 2nd graphic in the list and if you guess correctly on your 1st guess it displays the 1st graphic? I have made one Syntax error correction, in the while loop the 1st else condition used to read "letters_tried = letters_tried = letter" and now reads "letters_tried = letters_tried + letter". This has had no effect on the game as far as I can tell.... I think the problem is that an incorrect 1st guess calls the graphic indexed 1 and not 0....any thoughts?

– PyRMc
Nov 23 '18 at 7:22












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427875%2fhangman-game-indexerror-list-index-out-of-range%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427875%2fhangman-game-indexerror-list-index-out-of-range%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?