word = ["h","e","l","l","o"]
len1 = len(word)
blanks = ["_","_","_","_","_"]
print ''.join(word)
print ' '.join(blanks)
def lb():
guess = raw_input("Choose a letter. ")
count1 = 0
correct = 0
while count1 <= len1:
if guess == word[count1]:
blanks[count1] = guess
correct = 1
count1 = count1 + 1
lb()
if correct == 1: lb()
else: print "You lose"
print ''.join(word)
print ' '.join(blanks)
alright i wrote that to see if somethings would work the way i wanted them to. they didn't. when i run it i get an error. this is what it looks like.
hello
_ _ _ _ _
Choose a letter. h
Traceback (most recent call last):
File "C:\python2.4\hangman1.py", line 15, in -toplevel-
lb()
File "C:\python2.4\hangman1.py", line 11, in lb
if guess == word[count1]:
IndexError: list index out of range
it says the list index is out of range on line 11. that part should work. i've tested it outside of that program and it works. guess should equal h. count1 should equal 0. it should check to see if the first index in word is h. don't know whats wrong. i might just rewrite it using a for statement.
i copy and pasted from another place so i don't know how or why it looks the way it does...
while count1 <= len1 is a classical 'off by one' logical error
Subscript out of range means you are trying to access the array variable "word" out of it's original range you declared it for.
length of word = 5
word[0] = h
word[1] = e
......
word[4] = o
word[5] = subscript error
thanks for the help. i saw that 2 days after i posted.