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

hangman with strings and list methods

ilikepython
Expert 100+
P: 844
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:
Expand|Select|Wrap|Line Numbers
  1. c = input('Would you like to play hangman? Type y or n ')
  2.  
  3.  
  4.         word = sample(('soccer', 'robbery', 'antibiotics'), 1)
  5.         word = str(word)    
  6.         spaces = len(word) - 4
  7.         def hangman():
  8.             print '-------- '
  9.             print '|______| '
  10.             print ' O '
  11.             print ' O '
  12.             print ' O '
  13.             print ' O '
  14.             print ' O '
  15.             print '---- '
  16.             print '|___| '
  17.         def hangman1():
  18.             hangman()
  19.             m = '%s' % ('_ ' * spaces)
  20.             print m 
  21.             number = word.find(letter)
  22.             m.split()
  23.             m.replace('_', letter)
  24.  
Problems
1. I can't get python to replace the "blank" with the letter that my user chose.
I've tried a lot of things and I'm really getting frustrated.

2. When I'm running the program I always have to input something in quotes or it will give me some NameError or something. I've tried:
x = str('x')
and x = 'x' in a number of ways and it's not working. Please help

I'm runnning Python 2.5 on Windows Xp

Note: If you see the '-4' that's another little problem: when the spaces for the word appear it prints 4 more than the chosen word so I put '- 4'(not the best idea) if you can tell me how to fix that, that would be great.


Thanks
Feb 17 '07 #1
Share this Question
Share on Google+
18 Replies

bartonc
Expert 5K+
P: 6,596
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:
Expand|Select|Wrap|Line Numbers
  1. c = input('Would you like to play hangman? Type y or n ')
  2.  
  3.  
  4.         word = sample(('soccer', 'robbery', 'antibiotics'), 1)
  5.         word = str(word)    
  6.         spaces = len(word) - 4
  7.         def hangman():
  8.             print '-------- '
  9.             print '|______| '
  10.             print ' O '
  11.             print ' O '
  12.             print ' O '
  13.             print ' O '
  14.             print ' O '
  15.             print '---- '
  16.             print '|___| '
  17.         def hangman1():
  18.             hangman()
  19.             m = '%s' % ('_ ' * spaces)
  20.             print m 
  21.             number = word.find(letter)
  22.             m.split()
  23.             m.replace('_', letter)
  24.  
Problems
1. I can't get python to replace the "blank" with the letter that my user chose.
I've tried a lot of things and I'm really getting frustrated.

2. When I'm running the program I always have to input something in quotes or it will give me some NameError or something. I've tried:
x = str('x')
and x = 'x' in a number of ways and it's not working. Please help

I'm runnning Python 2.5 on Windows Xp

Note: If you see the '-4' that's another little problem: when the spaces for the word appear it prints 4 more than the chosen word so I put '- 4'(not the best idea) if you can tell me how to fix that, that would be great.


Thanks
Hello and welcome to the Python Forum on TSDN! Something weird happend when you pasted you code here, but we'll work on that as we go along.
#2 for strings, use
Expand|Select|Wrap|Line Numbers
  1. inputStr = raw_input("enter a string")
.
I'll look at the rest in a bit.
Feb 17 '07 #2

bartonc
Expert 5K+
P: 6,596
Hello and welcome to the Python Forum on TSDN! Something weird happend when you pasted you code here, but we'll work on that as we go along.
#2 for strings, use
Expand|Select|Wrap|Line Numbers
  1. inputStr = raw_input("enter a string")
.
I'll look at the rest in a bit.
Go ahead and post the entire code here. That will make things easier for everybody. Please read the "POSTING GUIDELINES" on the right hand side of the page to learn how to use code tags (the # simbol in this editor window). You can edit your post for 5 minutes to try to get it right. I can fix it if you are having trouble, so don't worry about that too much right now.
Feb 17 '07 #3

ilikepython
Expert 100+
P: 844
Go ahead and post the entire code here. That will make things easier for everybody. Please read the "POSTING GUIDELINES" on the right hand side of the page to learn how to use code tags (the # simbol in this editor window). You can edit your post for 5 minutes to try to get it right. I can fix it if you are having trouble, so don't worry about that too much right now.
Ok
Expand|Select|Wrap|Line Numbers
  1. import time
  2. from time import sleep
  3. import random
  4. from random import sample
  5.  
  6.  
  7. search = True
  8. while search:
  9.     try:
  10.  
  11.  
  12.         search = False
  13.         c = input('Would you like to play hangman? Type y or n ')
  14.  
  15.  
  16.         word = sample(('soccer', 'robbery', 'antibiotics'), 1)
  17.         word = str(word)    
  18.         spaces = len(word) - 4
  19.         def hangman():
  20.             print '-------- '
  21.             print '|______| '
  22.             print ' O '
  23.             print ' O '
  24.             print ' O '
  25.             print ' O '
  26.             print ' O '
  27.             print '---- '
  28.             print '|___| '
  29.         def hangman1():
  30.             hangman()
  31.             m = '%s' % ('_ ' * spaces)
  32.             print m 
  33.             number = word.find(letter)
  34.             m.split()
  35.             m.replace('_', letter)            
  36.  
  37.  
  38.  
  39.     except:
  40.         c = str('c') 
  41.     else:
  42.         pass
  43.  
  44.  
  45.         if c == 'y':
  46.             search = False        
  47.             sleep(2)
  48.             print "Alright, let's play "
  49.             hangman()
  50.  
  51.             print  '%s' % ('_ ' * spaces)
  52.  
  53.         else:
  54.             if c == 'n' :
  55.                 print 'Ok, Buh Bye then'
  56.                 sleep(2)
  57.                 exit()
  58.             else:
  59.                 c = str(c)
  60. search = True
  61. alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  62. while search:
  63.     letter = input("Pick a letter. A letter that is missing has already been used")
  64.  
  65.     print alphabet
  66.     if letter in word: 
  67.         print 'There was a %s in the word' % (letter)
  68.         num = alphabet.index(letter)
  69.         del alphabet[num]
  70.         print alphabet
  71.         hangman1()
  72.  
  73.     else:
  74.         if letter not in word:
  75.             print 'There was no %s in the word' % (letter)
  76.  
Feb 18 '07 #4

bartonc
Expert 5K+
P: 6,596
I didn't run this, nor did I work on hangman1() except to give you an idea for structure. This should give you something to go on. You have used many features of the language very well. Hopefully, this will give you some ideas of good structure.
Expand|Select|Wrap|Line Numbers
  1. # no need to import the whole module if all you want is one function
  2. from time import sleep
  3. from random import sample
  4. from string import lowercase as letters # string module has useful constants in it.
  5.  
  6.  
  7. wordTuple = ('soccer', 'robbery', 'antibiotics')
  8. maxGuesses = 5
  9.  
  10. def hangman():
  11.     print '-------- '
  12.     print '|______| '
  13.     print ' O '
  14.     print ' O '
  15.     print ' O '
  16.     print ' O '
  17.     print ' O '
  18.     print '---- '
  19.     print '|___| '
  20.  
  21.  
  22. def hangman1(w, m, c):
  23.     hangman()
  24. ##    m = '%s' % ('_ ' * spaces)
  25.     print w, m, c
  26. ##    number = word.find(letter)
  27. ##    m.split()
  28. ##    m.replace('_', letter)    
  29.  
  30.  
  31. while True:
  32.     charCount = 0
  33.     alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  34.                 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  35.  
  36.     answer = raw_input('Would you like to play hangman? Type y or n ')
  37.     if answer[0].lower() == 'n':
  38.         break
  39.  
  40.     word = sample(wordTuple, 1)[0]  # extract the word from sample()'s list
  41.     nSpaces = len(word)
  42.     blanks = '_' * nSpaces
  43.  
  44.     print "Alright, let's play "
  45.     sleep(2)
  46.     while charCount < maxGuesses:
  47.         hangman()
  48.         print alphabet
  49.         letter = ""
  50.         while letter not in letters:
  51.             letter = raw_input("Pick a letter. A letter that is missing has already been used").lower()
  52.  
  53.         if letter in word: 
  54.             print 'There was a \"%s\" in the word' % letter
  55.             blanks = hangman1(word, blanks, letter)
  56.  
  57.     ##        num = alphabet.index(letter) a list can do both at once
  58.     ##        del alphabet[num]
  59.             try:
  60.                 alphabet.remove(letter)
  61.             except IndexError:
  62.                 pass
  63.         else:
  64.             print 'There was no \"%s\" in the word' % letter
  65.         charCount += 1
  66.  
  67. print 'Ok, Buh Bye then'
  68. sleep(2)
  69. # exit() In python, just run off the end of the module
  70.  
  71.  
Feb 18 '07 #5

ilikepython
Expert 100+
P: 844
Thank you very much.
I appreciate your help.
I will see how it goes and post the finished script.

Thanks
Feb 18 '07 #6

bartonc
Expert 5K+
P: 6,596
Thank you very much.
I appreciate your help.
I will see how it goes and post the finished script.

Thanks
You are welcome. We'll help you get that hangman1() fuction working if you get stuck.

I forgot something:
Expand|Select|Wrap|Line Numbers
  1. def hangman1(w, m, c):
  2.     hangman()
  3. ##    m = '%s' % ('_ ' * spaces)
  4.     print w, m, c
  5. ##    number = word.find(letter)
  6. ##    m.split()
  7. ##    m.replace('_', letter)
  8.     return m  # send the result back to caller
Feb 18 '07 #7

ilikepython
Expert 100+
P: 844
One quick question.
If I have a while loop and a try\except block, can I return back to the beginning of the while loop if let's say an error occured.
For example:
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     letter = inputStr = raw_input("Pick a letter")
  3.     if letter in word:
  4.         try:
  5.             alphabet.remove(letter)
  6.         except ValueError:
  7.             print ' You already used this letter'
  8.  
can I return to the input after the ValueError even if there is more to the while loop after the try\except block??
Feb 18 '07 #8

bvdet
Expert Mod 2.5K+
P: 2,851
I hope you don't mind, but I have some suggestions.
Instead of removing letters from an alphabet list after each guess:
Expand|Select|Wrap|Line Numbers
  1.     guess_list = []
  2. ....................
  3.         guess_list.append(letter)
  4.         guess_list.sort()
  5.         print 'So far you have guessed the following letters: %s' % (','.join(guess_list))
How about displaying a hint string? Initialize it:
Expand|Select|Wrap|Line Numbers
  1.     hint_string = '_' * len(word)
Update the hint string:
Expand|Select|Wrap|Line Numbers
  1. def get_index(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
  11.  
  12. def hangman1(hint_str, letter, pos_list):
  13.     ......................
  14.     for i in pos_list:
  15.         lst = [hint_str[:i], ]
  16.         if len(hint_str) >= i+1:
  17.             lst.append(hint_str[(i+1):])
  18.         hint_str = letter.join(lst)
  19.     print 'Hint: %s' % (hint_str)
  20.     ........................
  21.     return hint_str
If the user guesses the same letter twice, tell him about it:
Expand|Select|Wrap|Line Numbers
  1.             if letter in hint_string:
  2.                 print "You have already guessed letter '%s' dodo!" % (letter)
  3.                 hint_string = hangman1(hint_string, letter, [])
Give the user a way out:
Expand|Select|Wrap|Line Numbers
  1.         letter = raw_input("Enter a letter or '?' if you are ready to solve the word.")
  2.         if letter == '?':
  3.             hint_string = raw_input("Give it your best shot!")
  4.             if hint_string == word:
  5.                 print "Congratulations! You have solved the word!"
  6.                 break
  7.             else:
  8.                 print "You have failed miserably!"
  9.                 break
Here's some output:
Expand|Select|Wrap|Line Numbers
  1. >>> Alright, let's play 
  2. --------
  3. |______|
  4.  O 
  5.  O 
  6.  O 
  7.  O 
  8.  O 
  9. ----
  10. |___|
  11. Hint: ______
  12. So far you have guessed the following letters: c
  13. There were 2 occurrences of c in the word.
  14. --------
  15. |______|
  16.  O 
  17.  O 
  18.  O 
  19.  O 
  20.  O 
  21. ----
  22. |___|
  23. Hint: __cc__
  24. You entered the word 'soccer'
  25. Congratulations! You have solved the word!
  26. >>>
Feb 18 '07 #9

bartonc
Expert 5K+
P: 6,596
One quick question.
If I have a while loop and a try\except block, can I return back to the beginning of the while loop if let's say an error occured.
For example:
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     letter = inputStr = raw_input("Pick a letter")
  3.     if letter in word:
  4.         try:
  5.             alphabet.remove(letter)
  6.         except ValueError:
  7.             print ' You already used this letter'
  8.  
can I return to the input after the ValueError even if there is more to the while loop after the try\except block??
Yes. That's exactly what the t/e block does. It keeps program flow going the way that you would expect (want) instead of jumping out due to errors.
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     # do stuff
  3.     if condition # this should include except clause
  4.         continue    # break's other half
I've never tried it, but I'll bet that you have by now.
Feb 18 '07 #10

bartonc
Expert 5K+
P: 6,596
I hope you don't mind, but I have some suggestions.
Instead of removing letters from an alphabet list after each guess:
Jump in any time, B. I really appreciate your input. Great suggestions here, again! Thanks.
Feb 18 '07 #11

ilikepython
Expert 100+
P: 844
Thanks all for your suggestions.
I will post back here when i finish the game.
Feb 19 '07 #12

bartonc
Expert 5K+
P: 6,596
Thanks all for your suggestions.
I will post back here when i finish the game.
It's a fun language, isn't it? Easy too, I think.
Feb 19 '07 #13

ilikepython
Expert 100+
P: 844
I hope you don't mind, but I have some suggestions.

Update the hint string:
Expand|Select|Wrap|Line Numbers
  1. def get_index(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
  11.  
  12. def hangman1(hint_str, letter, pos_list):
  13.     ......................
  14.     for i in pos_list:
  15.         lst = [hint_str[:i], ]
  16.         if len(hint_str) >= i+1:
  17.             lst.append(hint_str[(i+1):])
  18.         hint_str = letter.join(lst)
  19.     print 'Hint: %s' % (hint_str)
  20.     ........................
  21.     return hint_str

Could you please explain to me the get_index() and hangman1() functions.
I don't really understand what get_index is supposed to do.
Also, in hangman1() what's the "pos_list" supposed to be.
Sorry for my ignorence but I don't really get it.
Thanks for your suggestions.
Feb 19 '07 #14

bvdet
Expert Mod 2.5K+
P: 2,851
Could you please explain to me the get_index() and hangman1() functions.
I don't really understand what get_index is supposed to do.
Also, in hangman1() what's the "pos_list" supposed to be.
Sorry for my ignorence but I don't really get it.
Thanks for your suggestions.
Function 'get_index' returns a list of indices (positions) in the text string (word) that matches 'letter'. The function call 'get_index('soccer', 'c')' returns '[2, 3]' so I know there are 2 occurrances of 'c' in 'soccer' and I know the positions.
Expand|Select|Wrap|Line Numbers
  1.     for i in pos_list:
  2.         lst = [hint_str[:i], ]
  3.         if len(hint_str) >= i+1:
  4.             lst.append(hint_str[(i+1):])
  5.         hint_str = letter.join(lst)
  6.     print 'Hint: %s' % (hint_str)
  7.     return hint_str
Variable 'pos_list' (position list), obtained from function get_index(), is passed to hangman1(). We iterate on 'pos_list' to replace the '_' in hint_string with 'letter' in the correct position for each occurrance of 'letter' in 'word'. If we pass an empty list to hangman1(), no iteration takes place. We want to pass an empty list to hangman1() if:
1. The player guessed 'letter' previously.
2. 'letter' does not occur in 'word.
If you have more questions, please ask. HTH
Feb 19 '07 #15

P: 2
can i get the script for hang man as i tryed to understand but faild and try and understand it as i find it easyer
thanks
Mar 6 '07 #16

bartonc
Expert 5K+
P: 6,596
can i get the script for hang man as i tryed to understand but faild and try and understand it as i find it easyer
thanks
The original poster (ilikepython) didn't post his finished work and hasn't checked in since the 25th of February. But he may check in and see this. We can hope.
Or you can start your own project and start your own thread and get help with it here on the Python Forum. Welcome to TheScripts.com.
Mar 6 '07 #17

ilikepython
Expert 100+
P: 844
The original poster (ilikepython) didn't post his finished work and hasn't checked in since the 25th of February. But he may check in and see this. We can hope.
Or you can start your own project and start your own thread and get help with it here on the Python Forum. Welcome to TheScripts.com.
sorry guys i didnt have that much time so here it is:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. from time import *
  3. from random import sample
  4. from string import lowercase as letters
  5.  
  6. playlist = []
  7. maxGuesses = 6
  8. wordList = []
  9. wordlist = file("totalwordlist.txt","r")
  10. for line in wordlist.readlines():
  11.     line = line[:-1]
  12.     wordList.append(line.lower())
  13.  
  14. def hangman():
  15.     print ' ' * 30, '-------- '
  16.     print ' ' * 30, '|______| '
  17.     print ' ' * 30, ' O '
  18.     print ' ' * 30, ' O '
  19.     print ' ' * 30, ' O ' 
  20.     print ' ' * 30, ' O '
  21.     print ' ' * 30, ' O '
  22.     print ' ' * 30, '---- '
  23.     print ' ' * 30, '|___| '
  24. def occurrences(x,z):
  25.     count = 0
  26.     index = x.find(z)
  27.     while index != -1:
  28.         count += 1
  29.         start = index + 1
  30.         index = x.find(z, start)
  31.     print "There was %d occurences of the letter %s in the word" % (count, letter)
  32.     print '\a' * count
  33. def get_index(s, item, i=0):
  34.     i_list = []
  35.     while True:
  36.         try:
  37.             i = s.index(item, i)
  38.             i_list.append(i)
  39.             i += 1
  40.         except:
  41.             break
  42.     return i_list
  43. def hangman1(j, letter, pos_list): 
  44.     hangman2(charCount)
  45.     global base
  46.     j = base
  47.     newbase = base[:]
  48.     for i in pos_list:
  49.         lst = [newbase[:i], ]
  50.         if len(newbase) >= i+1:
  51.             lst.append(newbase[(i+1):])
  52.         newbase = letter.join(lst)    
  53.     base = newbase
  54.     print ' ' * 30, newbase
  55.     print '\n'
  56.     return base
  57. def hangman2(x):
  58.     if x == 0:
  59.         hangman()
  60.     if x == 1:
  61.         print ' ' * 30, '-------- '
  62.         print ' ' * 30, '|______| '
  63.         print ' ' * 30, ' O   o'
  64.         print ' ' * 30, ' O '
  65.         print ' ' * 30, ' O ' 
  66.         print ' ' * 30, ' O '
  67.         print ' ' * 30, ' O '
  68.         print ' ' * 30, '---- '
  69.         print ' ' * 30, '|___| '
  70.     if x == 2:
  71.         print ' ' * 30, '-------- '
  72.         print ' ' * 30, '|______| '
  73.         print ' ' * 30, ' O   o'
  74.         print ' ' * 30, ' O   |'
  75.         print ' ' * 30, ' O ' 
  76.         print ' ' * 30, ' O '
  77.         print ' ' * 30, ' O '
  78.         print ' ' * 30, '---- '
  79.         print ' ' * 30, '|___| '
  80.     if x == 3:
  81.         print ' ' * 30, '-------- '
  82.         print ' ' * 30, '|______| '
  83.         print ' ' * 30, ' O   o'
  84.         print ' ' * 30, " O  /|"
  85.         print ' ' * 30, ' O ' 
  86.         print ' ' * 30, ' O '
  87.         print ' ' * 30, ' O '
  88.         print ' ' * 30, '---- '
  89.         print ' ' * 30, '|___| '
  90.     if x == 4:
  91.         print ' ' * 30, '-------- '
  92.         print ' ' * 30, '|______| '
  93.         print ' ' * 30, ' O   o'
  94.         print ' ' * 30, " O  /|\\"
  95.         print ' ' * 30, ' O ' 
  96.         print ' ' * 30, ' O '
  97.         print ' ' * 30, ' O '
  98.         print ' ' * 30, '---- '
  99.         print ' ' * 30, '|___| '
  100.     if x == 5:
  101.         print ' ' * 30, '-------- '
  102.         print ' ' * 30, '|______| '
  103.         print ' ' * 30, ' O   o'
  104.         print ' ' * 30, " O  /|\\"
  105.         print ' ' * 30, " O  /" 
  106.         print ' ' * 30, ' O '
  107.         print ' ' * 30, ' O '
  108.         print ' ' * 30, '---- '
  109.         print ' ' * 30, '|___| '
  110.     if x == 6:
  111.         print ' ' * 30, '-------- '
  112.         print ' ' * 30, '|______| '
  113.         print ' ' * 30, ' O   o'
  114.         print ' ' * 30, " O  /|\\"
  115.         print ' ' * 30, " O  / \\" 
  116.         print ' ' * 30, ' O '
  117.         print ' ' * 30, ' O '
  118.         print ' ' * 30, '---- '
  119.         print ' ' * 30, '|___| '
  120. def statetest():
  121.     global state
  122.     if len(guesslist) == 1:
  123.         state = 'one letter'
  124.     else:
  125.         if len(guesslist) > 1:
  126.             state = 'the following letters'
  127. def search_win():
  128.     if base.find('_') == -1:
  129.         global charCount
  130.         print 'Congratulations!! You have won!!!!!!!!!!'
  131.         print '\a' * 3
  132.         timed = time()
  133.         finaltime = timed - timer
  134.         print 'It took you %d seconds to complete the word' % (finaltime)
  135.         playlist.append("win")
  136.         global play
  137.         play = False
  138.     else:
  139.         pass
  140. def losecheck():
  141.     global play
  142.     if charCount >= maxGuesses:
  143.         print 'You have failed to guess the word!!'  
  144.         print "The word was '%s'" % (word)
  145.         playlist.append("lose")
  146.         play = False
  147.     else:
  148.         pass
  149.  
  150. def playnum():
  151.     global times
  152.     if len(playlist) == 0:
  153.         times = 'hangman'
  154.     else:
  155.         if len(playlist) > 0:
  156.             times = 'again' 
  157.     return times
  158.  
  159.  
  160. while True:
  161.     play = True
  162.     charCount = 0
  163.     guesslist = []
  164.     word = sample(wordList, 1)[0]
  165.     word = str(word)  
  166.     gameword = ' '.join(word)
  167.     base = '_ ' * len(word)
  168.     playnum()
  169.  
  170.     print 'Would you like to play %s? ' % times
  171.     c = inputStr = raw_input()
  172.  
  173.  
  174.     if c == 'yes':    
  175.         print "Alright, let's play"
  176.         print '\n'
  177.         hangman2(charCount)
  178.         print '\n' 
  179.         print ' ' * 30, '_ ' * len(word)
  180.         print '\n'
  181.         print '\a' * 2
  182.         timer = time() 
  183.  
  184.     else:
  185.         if c == 'no':
  186.             print 'Ok, Buh Bye then'
  187.             sleep(15)
  188.             exit()
  189.  
  190.  
  191.  
  192.         else:
  193.             print '\n'
  194.             print 'Please only enter in yes or no'
  195.             print '\n'
  196.             continue
  197.     alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  198.  
  199.     while play:            
  200.         letter = inputStr = raw_input("Pick a letter or enter 'guess' to solve the word ")
  201.         if letter in guesslist:
  202.             print '\n'
  203.             print 'You have already guessed that letter'
  204.             print '\n'
  205.             print 'You have guessed %s so far: %s' % (state, ', '.join(guesslist))
  206.             print '\n'
  207.             continue    
  208.         if letter in alphabet:            
  209.             guesslist.append(letter)
  210.             if letter in word:
  211.                 occurrences(word, letter)
  212.                 pos = get_index (gameword, letter)                         
  213.                 hangman1(base, letter, pos)
  214.                 search_win()
  215.  
  216.             else:
  217.                 if letter not in word:
  218.                     statetest()
  219.                     print '\n'
  220.                     print 'There was no %s in the word' % (letter)
  221.                     print '\n'
  222.                     print 'You have guessed %s so far: %s' % (state, ', '.join(guesslist)) 
  223.                     print '\n'               
  224.                     charCount += 1
  225.                     print '\n'
  226.                     hangman2(charCount)
  227.                     print '\n'
  228.                     print ' ' * 30, base
  229.                     print '\n'
  230.                     losecheck()
  231.         elif letter not in alphabet:        
  232.             if letter == 'guess':
  233.                 print '\n'
  234.                 guess = inputStr = raw_input("Ok, so guess then ")
  235.                 if guess == word:
  236.                     print 'Congratulations!! You have won!!!!!!!!!!'
  237.                     print '\a' * 3
  238.                     timed = time()
  239.                     finaltime = timed - timer
  240.                     print '\n'
  241.                     print 'It took you %d seconds to complete the word' % (finaltime)
  242.                     print '\n'
  243.                     playlist.append("win")
  244.                     play = False
  245.                 elif guess != word:                
  246.                     print 'You have failed to guess the word!!'
  247.                     print '\n'
  248.                     print "The word was '%s'" % (word)
  249.                     print '\n'
  250.                     playlist.append("lose")
  251.                     play = False
  252.             print '\n'
  253.             print 'Please enter in a letter from the alphabet'
  254.             print '\n'
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. sleep(20)
  267. exit()
  268.  
uhhh yea its really kind of messy but as soon as it worked the way i wanted it to I started working on something else so...
Mar 6 '07 #18

bartonc
Expert 5K+
P: 6,596
Thanks, ilikepython. It's very nice of you to drop in once in a while.
Mar 7 '07 #19

Post your reply

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