By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,926 Members | 1,806 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,926 IT Pros & Developers. It's quick & easy.

Hangman code

P: 21
Making a hangman code (I'm a n00b, still in the tutorial stage)

Anyway, here's my (top-secret) code:
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     choice=raw_input("Guess!(lowercase only please!)")
  3.     if len(choice)>1:
  4.         print "Don't cheat!"
  5.         guess()
  6.     elif guess in y:
  7.         b[z.find(choice)]=choice
  8.         print "Good Choice!"
  9.         print ''.join(b)
  10.         guess()
  11.  
  12. def main():
  13.     x=raw_input("Type a name")
  14.     y=x.lower().split(' ')
  15.     z=''.join(y)
  16.     print z
  17.     b=['_ ']*len(z)
  18.     print ''.join(b)
  19.     guess()
  20.  
  21. if __name__=='__main__':
  22.     main()
  23.  
Anyways, I get down to line 6 and it tells me that global name z is not defined. I'm guessing I have to put the z thing in there, but I don't know how.

What should I do?
P.S. the code is still in development. That is why it is not finished.
Mar 17 '07 #1
Share this Question
Share on Google+
24 Replies


bartonc
Expert 5K+
P: 6,596
Making a hangman code (I'm a n00b, still in the tutorial stage)

Anyway, here's my (top-secret) code:
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     choice=raw_input("Guess!(lowercase only please!)")
  3.     if len(choice)>1:
  4.         print "Don't cheat!"
  5.         guess()
  6.     elif guess in y:
  7.         b[z.find(choice)]=choice
  8.         print "Good Choice!"
  9.         print ''.join(b)
  10.         guess()
  11.  
  12. def main():
  13.     x=raw_input("Type a name")
  14.     y=x.lower().split(' ')
  15.     z=''.join(y)
  16.     print z
  17.     b=['_ ']*len(z)
  18.     print ''.join(b)
  19.     guess()
  20.  
  21. if __name__=='__main__':
  22.     main()
  23.  
Anyways, I get down to line 6 and it tells me that global name z is not defined. I'm guessing I have to put the z thing in there, but I don't know how.

What should I do?
P.S. the code is still in development. That is why it is not finished.
Hi St33med. Most code we see here is "still in development"; we're here to help you get it working. So, welcome to the Python help forum on TSDN!

This is an issue of "scope". The scope rules say that variables assigned in a function are "local" to that function. I quick way to make this work is (but maybe not the best):
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     choice=raw_input("Guess!(lowercase only please!)")
  3.     if len(choice)>1:
  4.         print "Don't cheat!"
  5.         guess()
  6.     elif guess in y:
  7.         b[z.find(choice)]=choice
  8.         print "Good Choice!"
  9.         print ''.join(b)
  10.         guess()
  11.  
  12. if __name__=='__main__':
  13.     x=raw_input("Type a name")
  14.     y=x.lower().split(' ')
  15.     z=''.join(y)
  16.     print z
  17.     b=['_ ']*len(z)
  18.     print ''.join(b)
  19.     guess()
  20.  
  21.  
I'm in a rush at this second, but I'll have some time in just a bit...
Thanks for joining.
Mar 17 '07 #2

bartonc
Expert 5K+
P: 6,596
Hi St33med. Most code we see here is "still in development"; we're here to help you get it working. So, welcome to the Python help forum on TSDN!

This is an issue of "scope". The scope rules say that variables assigned in a function are "local" to that function. I quick way to make this work is (but maybe not the best):
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     choice=raw_input("Guess!(lowercase only please!)")
  3.     if len(choice)>1:
  4.         print "Don't cheat!"
  5.         guess()
  6.     elif guess in y:
  7.         b[z.find(choice)]=choice
  8.         print "Good Choice!"
  9.         print ''.join(b)
  10.         guess()
  11.  
  12. if __name__=='__main__':
  13.     x=raw_input("Type a name")
  14.     y=x.lower().split(' ')
  15.     z=''.join(y)
  16.     print z
  17.     b=['_ ']*len(z)
  18.     print ''.join(b)
  19.     guess()
  20.  
  21.  
I'm in a rush at this second, but I'll have some time in just a bit...
Thanks for joining.
What this does is put the varaibles x, y and z into the module scope. As such, they can be "seen" by functions that are also in that scope. This use of "global" scope variables is good for learning because it is fairly simple. There are lot's of other things to change before we get to the final working program, but let's take it one step at a time. And HAVE FUN.
Mar 17 '07 #3

P: 21
Well, the code worked and I made some enhancements:
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     choice=raw_input("Guess!(lowercase only please!)")
  3.     a=type.find(str(choice))
  4.     if len(choice)>1:
  5.         print "Don't cheat!"
  6.         guess()
  7.     elif a!=-1:
  8.         b[a]=str(choice)
  9.         print "Good Choice!"
  10.         c=''.join(b)
  11.         print c
  12.         if type==c:
  13.             print "You Win!"
  14.         else:
  15.             guess()
  16.     else:
  17.         d+=1
  18.         e[d]=choice
  19.         if d==0:
  20.             print "O",e
  21.             guess()
  22.         elif d==1:
  23.             print e,"O<"
  24.             guess()
  25.         elif d==2:
  26.             print e,"0<-"
  27.             guess()
  28.         elif d==3:
  29.             print e,"O<-<\n Game Over"
  30.  
  31. if __name__=='__main__':
  32.     type=raw_input("Type a name:")
  33.     type=''.join(type.lower().split(' '))
  34.     #print type
  35.     b=['_ ']*len(type)
  36.     print ''.join(b)
  37.     d=-1
  38.     e=['']*5
  39.     guess()
The problem with this code is that it tells me that d was referenced before assignment. If I put d in def guess, then it will always return d to -1 and the guy taking guesses will have infinite amount of guesses. That might have been why you said that the solution was not the best. What should I do?
Mar 18 '07 #4

Expert 100+
P: 511
Well, the code worked and I made some enhancements:
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     choice=raw_input("Guess!(lowercase only please!)")
  3.     a=type.find(str(choice))
  4.     if len(choice)>1:
  5.         print "Don't cheat!"
  6.         guess()
  7.     elif a!=-1:
  8.         b[a]=str(choice)
  9.         print "Good Choice!"
  10.         c=''.join(b)
  11.         print c
  12.         if type==c:
  13.             print "You Win!"
  14.         else:
  15.             guess()
  16.     else:
  17.         d+=1
  18.         e[d]=choice
  19.         if d==0:
  20.             print "O",e
  21.             guess()
  22.         elif d==1:
  23.             print e,"O<"
  24.             guess()
  25.         elif d==2:
  26.             print e,"0<-"
  27.             guess()
  28.         elif d==3:
  29.             print e,"O<-<\n Game Over"
  30.  
  31. if __name__=='__main__':
  32.     type=raw_input("Type a name:")
  33.     type=''.join(type.lower().split(' '))
  34.     #print type
  35.     b=['_ ']*len(type)
  36.     print ''.join(b)
  37.     d=-1
  38.     e=['']*5
  39.     guess()
The problem with this code is that it tells me that d was referenced before assignment. If I put d in def guess, then it will always return d to -1 and the guy taking guesses will have infinite amount of guesses. That might have been why you said that the solution was not the best. What should I do?
in your guess() function, 'd' is never 'initialized' before you do d+=1.
Either you pass 'd' value into guess(), as in guess(d) , or use global.
Mar 18 '07 #5

bartonc
Expert 5K+
P: 6,596
What my friend ghostdog74 means is:
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     global d
  3.     choice=raw_input("Guess!(lowercase only please!)")
  4.     a=type.find(str(choice))
  5.     if len(choice)>1:
  6.         print "Don't cheat!"
  7.         guess()
  8.     elif a!=-1:
  9.         b[a]=str(choice)
  10.         print "Good Choice!"
  11.         c=''.join(b)
  12.         print c
  13.         if type==c:
  14.             print "You Win!"
  15.         else:
  16.             guess()
  17.     else:
  18.         d+=1
  19.         e[d]=choice
  20.         if d==0:
  21.             print "O",e
  22.             guess()
  23.         elif d==1:
  24.             print e,"O<"
  25.             guess()
  26.         elif d==2:
  27.             print e,"0<-"
  28.             guess()
  29.         elif d==3:
  30.             print e,"O<-<\n Game Over"
  31.  
  32. if __name__=='__main__':
  33.     type=raw_input("Type a name:")
  34.     type=''.join(type.lower().split(' '))
  35.     #print type
  36.     b=['_ ']*len(type)
  37.     print ''.join(b)
  38.     d=-1
  39.     e=['']*5
  40.     guess()
  41.  
It's very cute! I ran it and it work using this global scope technique. You a really quit a good pythoneer. Keep it up! Next you may want to play with parameter passing (also as GD has suggested).
Mar 18 '07 #6

Expert 100+
P: 511
Just an opinion of mine, its better to use while loop for tasks like this:
for example:
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.          ask for input
  3.          if input is quit or exit:
  4.                 break
  5.          else:
  6.                 call your functions.
  7.  
In the original code, guess() is defined as a function, but in it, there are also calls to guess() itself. In programming concepts, this is something like recursion. Before the first call to guess() has returned to caller, another guess() gets called and so on. The stack gets slowly "filled up" due to storing of return addresses and etc etc......at least that's how i feel.
Mar 18 '07 #7

bvdet
Expert Mod 2.5K+
P: 2,851
What my friend ghostdog74 means is:
Expand|Select|Wrap|Line Numbers
  1. def guess():
  2.     global d
  3.     choice=raw_input("Guess!(lowercase only please!)")
  4.     a=type.find(str(choice))
  5.     if len(choice)>1:
  6.         print "Don't cheat!"
  7.         guess()
  8.     elif a!=-1:
  9.         b[a]=str(choice)
  10.         print "Good Choice!"
  11.         c=''.join(b)
  12.         print c
  13.         if type==c:
  14.             print "You Win!"
  15.         else:
  16.             guess()
  17.     else:
  18.         d+=1
  19.         e[d]=choice
  20.         if d==0:
  21.             print "O",e
  22.             guess()
  23.         elif d==1:
  24.             print e,"O<"
  25.             guess()
  26.         elif d==2:
  27.             print e,"0<-"
  28.             guess()
  29.         elif d==3:
  30.             print e,"O<-<\n Game Over"
  31.  
  32. if __name__=='__main__':
  33.     type=raw_input("Type a name:")
  34.     type=''.join(type.lower().split(' '))
  35.     #print type
  36.     b=['_ ']*len(type)
  37.     print ''.join(b)
  38.     d=-1
  39.     e=['']*5
  40.     guess()
  41.  
It's very cute! I ran it and it work using this global scope technique. You a really quit a good pythoneer. Keep it up! Next you may want to play with parameter passing (also as GD has suggested).
It does work well. However, if you enter a word that has two of the same letter as in "sell", you can never solve it. You should not use a variable name that masks a Python built-in function (type). We had another thread on Hangman recently, and we used this function to determine the occurances of a letter in a word:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i=0):
  2.     i_list = []
  3.     while True:
  4.         try:
  5.             i = s.index(item, i)
  6.             i_list.append(i)
  7.             i += 1
  8.         except:
  9.             break
  10.     return i_list
I agree with ghostdog74 abot the use of a loop instead of recursion. I also encourage you to avoid using global variables where possible. As in the function listed above, the word (s) and letter (item) are passed to get_index() and in return we receive a list of indices or an empty list if there were no occurances of 'item' in 's':
Expand|Select|Wrap|Line Numbers
  1. >>> iLst = indexList('mississippi', 's')
  2. >>> iLst
  3. [2, 3, 5, 6]
  4. >>> hint_string = '_'*len('mississippi')
  5. >>> for i in iLst:
  6. ...     lst = [hint_string[:i], ]
  7. ...     if len(hint_string) >= i+1:
  8. ...         lst.append(hint_string[(i+1):])
  9. ...     hint_string = 's'.join(lst)
  10. ...     
  11. >>> hint_string
  12. '__ss_ss____'
  13. >>> 
Mar 18 '07 #8

P: 21
Well here is my code, almost finished:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. def guess(d,x,e):
  3.             choice=raw_input("Guess!(lowercase only please!)")
  4.             a=type.find(str(choice))
  5.             if len(choice)>1:
  6.                 print "Don't cheat!"
  7.                 guess(d,x,e)
  8.             elif a!=-1:
  9.                 b[a]=str(choice)
  10.                 print "Good Choice!"
  11.                 c=''.join(b)
  12.                 print c, e
  13.                 if type==c:
  14.                     print "You Win!"
  15.                     sys.exit()
  16.                 else:
  17.                     guess(d,x,e)
  18.             else:
  19.                 d+=x
  20.                 if guess in e:
  21.                     print "You have already guessed this!"
  22.                     guess(d,x,e)
  23.                 e[d]=choice
  24.                 if d==0:
  25.                     print "O"
  26.                     x=1
  27.                 elif d==1:
  28.                     print "O<"
  29.                     x=2
  30.                 elif d==2:
  31.                     print "0<-"
  32.                     x=3
  33.                 elif d==3:
  34.                     print "O<-<\n Game Over"
  35.                     sys.exit()
  36.                 print "Letters Guessed: ", ' '.join(e)
  37.                 guess(d,x,e)
  38. if __name__=='__main__':
  39.     type=raw_input("Type a name:")
  40.     type=''.join(type.lower().split(' '))
  41.     #print type
  42.     b=['_ ']*len(type)
  43.     print ''.join(b)
  44.     d=0
  45.     x=0
  46.     e=['']*4
  47.     guess(d,x,e)
  48.  
  49.  
Where should I put this indexList?
Mar 18 '07 #9

bvdet
Expert Mod 2.5K+
P: 2,851
Well here is my code, almost finished:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. def guess(d,x,e):
  3.             choice=raw_input("Guess!(lowercase only please!)")
  4.             a=type.find(str(choice))
  5.             if len(choice)>1:
  6.                 print "Don't cheat!"
  7.                 guess(d,x,e)
  8.             elif a!=-1:
  9.                 b[a]=str(choice)
  10.                 print "Good Choice!"
  11.                 c=''.join(b)
  12.                 print c, e
  13.                 if type==c:
  14.                     print "You Win!"
  15.                     sys.exit()
  16.                 else:
  17.                     guess(d,x,e)
  18.             else:
  19.                 d+=x
  20.                 if guess in e:
  21.                     print "You have already guessed this!"
  22.                     guess(d,x,e)
  23.                 e[d]=choice
  24.                 if d==0:
  25.                     print "O"
  26.                     x=1
  27.                 elif d==1:
  28.                     print "O<"
  29.                     x=2
  30.                 elif d==2:
  31.                     print "0<-"
  32.                     x=3
  33.                 elif d==3:
  34.                     print "O<-<\n Game Over"
  35.                     sys.exit()
  36.                 print "Letters Guessed: ", ' '.join(e)
  37.                 guess(d,x,e)
  38. if __name__=='__main__':
  39.     type=raw_input("Type a name:")
  40.     type=''.join(type.lower().split(' '))
  41.     #print type
  42.     b=['_ ']*len(type)
  43.     print ''.join(b)
  44.     d=0
  45.     x=0
  46.     e=['']*4
  47.     guess(d,x,e)
  48.  
  49.  
Where should I put this indexList?
Anywhere above "if __name__ == '__main__':"
Mar 18 '07 #10

P: 21
Anywhere above "if __name__ == '__main__':"
And then put the indexList in what part of the code?
Mar 18 '07 #11

bvdet
Expert Mod 2.5K+
P: 2,851
And then put the indexList in what part of the code?
You will need to rework your code somewhat, because you have a list of indices instead of a single index. Here's a start:
Expand|Select|Wrap|Line Numbers
  1.         def guess(d,x,e):
  2.             choice = raw_input("Guess!(lowercase only please!)")
  3.             aList = indexList(word, choice)
  4.             ...........................
  5.             elif len(aList) > 0:
  6.                 print 'There are %d occurrances of %s in %s' % (len(aList), choice, word)
Notice that I am using 'word' instead of 'type'.
Mar 18 '07 #12

P: 21
Need an explanation of what indexList does. I'm just having a hard time following what it does.
Mar 18 '07 #13

P: 21
Never mind about last. The problem is when I put aList as the element in b (as in b[aList]=str(choice)) , it tells me that aList needs to be an integer, not a list. How do I transform aList into an integer?
Mar 18 '07 #14

bartonc
Expert 5K+
P: 6,596
Need an explanation of what indexList does. I'm just having a hard time following what it does.
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i=0):
  2.     i_list = []
  3.     while True:
  4.         try:
  5.             i = s.index(item, i)
  6.             i_list.append(i)
  7.             i += 1
  8.         except:
  9.             break
  10.     return i_list
returns a list of inexes for every occurance of item in s.
Mar 18 '07 #15

bartonc
Expert 5K+
P: 6,596
Never mind about last. The problem is when I put aList as the element in b (as in b[aList]=str(choice)) , it tells me that aList needs to be an integer, not a list. How do I transform aList into an integer?
I'm having a hard time following along because your variable names are not very descriptive. If b (could use a better name) is a list, why not somethingbetterList?
If aList is a list of indexes, you need to
Expand|Select|Wrap|Line Numbers
  1. b[aList][indexintothislist] = str(choice)
Mar 18 '07 #16

P: 21
Alright here is the more understandable code:
Expand|Select|Wrap|Line Numbers
  1. import sys # used to exit
  2. def guess(count,addCount,letters):
  3.             choice=raw_input("Guess!(lowercase only please!)")
  4.             aList=indexList(word, choice)
  5.             print aList
  6.             if len(choice)>1:
  7.                 print "Don't cheat!" # Exactly :)
  8.                 guess(count,addCount,letters)
  9.             elif aList!=[-1]:
  10.                 line[aList]=str(choice) #Problem here. aList is list when needs to be integers
  11.                 print "Good Choice!"
  12.                 c=''.join(line)
  13.                 print c,"Letters missed:", letters #Tells what letters the guy has missed
  14.                 if word==c:
  15.                     print "You Win!" 
  16.                     sys.exit()
  17.                 else:
  18.                     guess(count,addCount,letters) #repeats until win or lose
  19.             else:
  20.                 if choice in letters:
  21.                     print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
  22.                     guess(count,addCount,letters)
  23.                 count+=addCount
  24.                 letters[count]=choice
  25.                 if count==0:
  26.                     print "O"
  27.                     addCount=1
  28.                 elif count==1:
  29.                     print "O<"
  30.                     addCount=2
  31.                 elif count==2:
  32.                     print "0<-"
  33.                     addCount=3
  34.                 elif count==3:
  35.                     print "O<-<\n Game Over XP"
  36.                     sys.exit()
  37.                 print "Letters missed: ", ' '.join(letters)
  38.                 guess(count,addCount,letters)
  39.  
  40. def indexList(s, item, i=0):
  41.     i_list = []
  42.     while True:
  43.         try:
  44.             i = s.index(item, i)
  45.             i_list.append(i)
  46.             i += 1
  47.         except:
  48.             i_list.append(-1)
  49.             break
  50.     return i_list
  51.  
  52. if __name__=='__main__':
  53.     word=raw_input("Type a name:")
  54.     word=''.join(word.lower().split(' '))
  55.     #print word #This was just for testing
  56.     line=['_ ']*len(word)
  57.     print ''.join(line)
  58.     count=0 #set count
  59.     addCount=0 #set add count
  60.     letters=['']*4 #make an empty list
  61.     guess(count,addCount,letters)
  62.  
  63.  
As it says in line 10, I need to find out how to make aList an integer because line will not recognize the elements inside a list.
Hmmmmm.....
Mar 18 '07 #17

P: 21
Whoops! What I meant to say was what do you put inside the second [], bartonc?
Mar 18 '07 #18

bartonc
Expert 5K+
P: 6,596
Alright here is the more understandable code:
Expand|Select|Wrap|Line Numbers
  1. import sys # used to exit
  2. def guess(count,addCount,letters):
  3.             choice=raw_input("Guess!(lowercase only please!)")
  4.             aList=indexList(word, choice)
  5.             print aList
  6.             if len(choice)>1:
  7.                 print "Don't cheat!" # Exactly :)
  8.                 guess(count,addCount,letters)
  9.             elif aList!=[-1]:
  10.                 line[aList]=str(choice) #Problem here. aList is list when needs to be integers
  11.                 print "Good Choice!"
  12.                 c=''.join(line)
  13.                 print c,"Letters missed:", letters #Tells what letters the guy has missed
  14.                 if word==c:
  15.                     print "You Win!" 
  16.                     sys.exit()
  17.                 else:
  18.                     guess(count,addCount,letters) #repeats until win or lose
  19.             else:
  20.                 if choice in letters:
  21.                     print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
  22.                     guess(count,addCount,letters)
  23.                 count+=addCount
  24.                 letters[count]=choice
  25.                 if count==0:
  26.                     print "O"
  27.                     addCount=1
  28.                 elif count==1:
  29.                     print "O<"
  30.                     addCount=2
  31.                 elif count==2:
  32.                     print "0<-"
  33.                     addCount=3
  34.                 elif count==3:
  35.                     print "O<-<\n Game Over XP"
  36.                     sys.exit()
  37.                 print "Letters missed: ", ' '.join(letters)
  38.                 guess(count,addCount,letters)
  39.  
  40. def indexList(s, item, i=0):
  41.     i_list = []
  42.     while True:
  43.         try:
  44.             i = s.index(item, i)
  45.             i_list.append(i)
  46.             i += 1
  47.         except:
  48.             i_list.append(-1)
  49.             break
  50.     return i_list
  51.  
  52. if __name__=='__main__':
  53.     word=raw_input("Type a name:")
  54.     word=''.join(word.lower().split(' '))
  55.     #print word #This was just for testing
  56.     line=['_ ']*len(word)
  57.     print ''.join(line)
  58.     count=0 #set count
  59.     addCount=0 #set add count
  60.     letters=['']*4 #make an empty list
  61.     guess(count,addCount,letters)
  62.  
  63.  
As it says in line 10, I need to find out how to make aList an integer because line will not recognize the elements inside a list.
Hmmmmm.....
I'm guessing that you want something like
Expand|Select|Wrap|Line Numbers
  1.                 line[aList][count]=str(choice) # index into a list of indexes
Mar 18 '07 #19

bartonc
Expert 5K+
P: 6,596
Whoops! What I meant to say was what do you put inside the second [], bartonc?
Hah, you beat me to it (by a few seconds).
Mar 18 '07 #20

P: 21
Weird. [count] obviously does nothing for it still gives me a TypeError.
Specifically, the TypeError looks like this:
Expand|Select|Wrap|Line Numbers
  1. TypeError: list indices must be integers
I might have read this wrong.
Mar 18 '07 #21

bvdet
Expert Mod 2.5K+
P: 2,851
Weird. [count] obviously does nothing for it still gives me a TypeError.
Specifically, the TypeError looks like this:
Expand|Select|Wrap|Line Numbers
  1. TypeError: list indices must be integers
I might have read this wrong.
Expand|Select|Wrap|Line Numbers
  1. elif len(aList) > 0:
  2.     for i in aList:
  3.         line[i] = choice       # is not choice already a string?
  4.         ..............................................................................
Mar 19 '07 #22

P: 21
Expand|Select|Wrap|Line Numbers
  1. elif len(aList) > 0:
  2.     for i in aList:
  3.         line[i] = choice       # is not choice already a string?
  4.         ..............................................................................
Yea! That worked! Before, I had to put choice as a string for some reason. It returned some type of error. But when I put in your code without the str before choice, it worked. Huh. I might've been seeing things.

Anyway, thank you for helping me! And, for those of you who want to see the final code, here it is:
Expand|Select|Wrap|Line Numbers
  1. import sys # used to exit
  2. def guess(count,addCount,letters):
  3.             choice=raw_input("Guess!(lowercase only please!)")
  4.             aList=indexList(word, choice)
  5.             print aList
  6.             if len(choice)>1:
  7.                 print "Don't cheat!" # Exactly :)
  8.                 guess(count,addCount,letters)
  9.             elif len(aList) > 0:
  10.                 for i in aList:
  11.                     line[i] = choice  #Problem here. aList is list when needs to be integers
  12.                     print "Good Choice!"
  13.                     c=''.join(line)
  14.                     print c,"Letters missed:", letters #Tells what letters the guy has missed
  15.                     if word==c:
  16.                         print "You Win!" 
  17.                         sys.exit()
  18.                     else:
  19.                         guess(count,addCount,letters) #repeats until win or lose
  20.             else:
  21.                 if choice in letters:
  22.                     print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
  23.                     guess(count,addCount,letters)
  24.                 count+=addCount
  25.                 letters[count]=choice
  26.                 if count==0:
  27.                     print "O"
  28.                     addCount=1
  29.                 elif count==1:
  30.                     print "O<"
  31.                     addCount=2
  32.                 elif count==2:
  33.                     print "0<-"
  34.                     addCount=3
  35.                 elif count==3:
  36.                     print "O<-<\n Game Over XP"
  37.                     sys.exit()
  38.                 print "Letters missed: ", ' '.join(letters)
  39.                 guess(count,addCount,letters)
  40.  
  41. def indexList(s, item, i=0):
  42.     i_list = []
  43.     while True:
  44.         try:
  45.             i = s.index(item, i)
  46.             i_list.append(i)
  47.             i += 1
  48.         except:
  49.             break
  50.     return i_list
  51.  
  52. if __name__=='__main__':
  53.     word=raw_input("Type a name:")
  54.     word=''.join(word.lower().split(' '))
  55.     #print word #This was just for testing
  56.     line=['_ ']*len(word)
  57.     print ''.join(line)
  58.     count=0 #set count
  59.     addCount=0 #set add count
  60.     letters=['']*4 #make an empty list
  61.     guess(count,addCount,letters) #carries everything to guess for later use
  62.  
Mar 19 '07 #23

bvdet
Expert Mod 2.5K+
P: 2,851
Yea! That worked! Before, I had to put choice as a string for some reason. It returned some type of error. But when I put in your code without the str before choice, it worked. Huh. I might've been seeing things.

Anyway, thank you for helping me! And, for those of you who want to see the final code, here it is:
Expand|Select|Wrap|Line Numbers
  1. import sys # used to exit
  2. def guess(count,addCount,letters):
  3.             choice=raw_input("Guess!(lowercase only please!)")
  4.             aList=indexList(word, choice)
  5.             print aList
  6.             if len(choice)>1:
  7.                 print "Don't cheat!" # Exactly :)
  8.                 guess(count,addCount,letters)
  9.             elif len(aList) > 0:
  10.                 for i in aList:
  11.                     line[i] = choice  #Problem here. aList is list when needs to be integers
  12.                     print "Good Choice!"
  13.                     c=''.join(line)
  14.                     print c,"Letters missed:", letters #Tells what letters the guy has missed
  15.                     if word==c:
  16.                         print "You Win!" 
  17.                         sys.exit()
  18.                     else:
  19.                         guess(count,addCount,letters) #repeats until win or lose
  20.             else:
  21.                 if choice in letters:
  22.                     print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
  23.                     guess(count,addCount,letters)
  24.                 count+=addCount
  25.                 letters[count]=choice
  26.                 if count==0:
  27.                     print "O"
  28.                     addCount=1
  29.                 elif count==1:
  30.                     print "O<"
  31.                     addCount=2
  32.                 elif count==2:
  33.                     print "0<-"
  34.                     addCount=3
  35.                 elif count==3:
  36.                     print "O<-<\n Game Over XP"
  37.                     sys.exit()
  38.                 print "Letters missed: ", ' '.join(letters)
  39.                 guess(count,addCount,letters)
  40.  
  41. def indexList(s, item, i=0):
  42.     i_list = []
  43.     while True:
  44.         try:
  45.             i = s.index(item, i)
  46.             i_list.append(i)
  47.             i += 1
  48.         except:
  49.             break
  50.     return i_list
  51.  
  52. if __name__=='__main__':
  53.     word=raw_input("Type a name:")
  54.     word=''.join(word.lower().split(' '))
  55.     #print word #This was just for testing
  56.     line=['_ ']*len(word)
  57.     print ''.join(line)
  58.     count=0 #set count
  59.     addCount=0 #set add count
  60.     letters=['']*4 #make an empty list
  61.     guess(count,addCount,letters) #carries everything to guess for later use
  62.  
You have an incorrect indentation at a critical point which prevents the script from executing properly. Try 'mississippi'. Once you make this correction, you are looking good!
Mar 19 '07 #24

P: 21
Whoops! I indented everything after the for statement, making it run only once. Fixed that and it now runs like a charm!
Again, Thanks!

EDIT: You beat me to it. Darn! :D
Mar 19 '07 #25

Post your reply

Sign in to post your reply or Sign up for a free account.