473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Hangman code

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
24 15911
bartonc
6,596 Expert 4TB
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
6,596 Expert 4TB
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
St33med
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
ghostdog74
511 Expert 256MB
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
6,596 Expert 4TB
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
ghostdog74
511 Expert 256MB
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
2,851 Expert Mod 2GB
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
St33med
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
2,851 Expert Mod 2GB
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
St33med
21
Anywhere above "if __name__ == '__main__':"
And then put the indexList in what part of the code?
Mar 18 '07 #11
bvdet
2,851 Expert Mod 2GB
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
St33med
21
Need an explanation of what indexList does. I'm just having a hard time following what it does.
Mar 18 '07 #13
St33med
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
St33med
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
St33med
21
Whoops! What I meant to say was what do you put inside the second [], bartonc?
Mar 18 '07 #18
bartonc
6,596 Expert 4TB
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
6,596 Expert 4TB
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
St33med
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
2,851 Expert Mod 2GB
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
St33med
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
2,851 Expert Mod 2GB
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
St33med
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

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

Similar topics

5
by: tigrfire | last post by:
So I'm trying to write a hangman game and the output is coming out a little strange. Here's my code thus far: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> ...
18
ilikepython
by: ilikepython | last post by:
Hi I'm new to Python(3 to 4 days) and I'm working on a hangman game and have been having some problems that really annoy me. Here is part of my script: c = input('Would you like to play hangman?...
4
by: princessfrost | last post by:
Hi! I was wondering if someone could please help me with a hangman program that I have to do. I have some ideas, but really don't know what to do or where to start. My program needs to be:...
47
by: araujo2nd | last post by:
Originally Posted by araujo2nd my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by...
3
by: kaka_hunter | last post by:
#include <iostream> #include <fstream> using namespace std; const int max_tries=7; int earnings=0; int wordnum; void getword () { ifstream fin;
2
by: tesa | last post by:
I am not able to figure out how to make this work. I am trying to create a hangman game. I am in a basic javascripting class. I am to only use very basic code as you can see. I am able to use any...
0
by: Madmartigan | last post by:
Hi I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way. I have included my code thus...
8
by: tidiz | last post by:
Hi, I'm trying to make a hangman game that should look like this: Welcome to Hangman ______ Your guess: c Success! __cc__ Your guess: b
1
by: AlexSc | last post by:
Hi to All, I am doing on a hangman project, Where this is a very simple one, where player would guess the word and if it is correct the letter would be from "-" to the correct letter once all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.