Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. sAnswer = ##( a word imported from a dictionary text file)
  2. sLetterGuessed = " "
  3. sLetterUsed = " "
  4.  
  5. sReveal = len(sAnswer) * "_ "
  6.  
  7. while True:
  8. sLetterGuessed = raw_input("Enter a letter or guess the word: ").lower()
  9.  
  10. while True:
  11. if sLetterGuessed in sLettersUsed:
  12. sLettersUsed += sLetterGuessed
  13. print "can't use that"
  14. break
  15. elif sLetterGuessed in sAnswer:
  16. sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
  17. sLettersUsed += sLetterGuessed
  18. print (sReveal)
  19. print HangingMan[iErrors]
  20. break
  21. elif sLetterGuessed not in sAnswer:
  22. sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
  23. sLettersUsed += sLetterGuessed
  24.  
  25. break
  26.  
  27. ## Hangman game
  28. ## A random word for example "pizza" will become "_ _ _ _ _ " Player will guess "z" output will be "_ _ z z _ " The current code does show this, but as it's in a loop it will always reset back to blank underscores. If I guess again, "a" it should be "_ _ z z a " but that's not what's happening it becomes "_ _ _ _ a " instead. Please help me with this ordeal !
  29.  
  30. sAnswer = ##( a word imported from a dictionary text file)
  31. sLetterGuessed = " "
  32. # string containing all of the letters guessed
  33. sLetterUsed = " "
  34. # string containing all of the letters guessed correctly
  35.  
  36. sReveal = len(sAnswer) * "_ "
  37. # ln(sAnswer) will return a number, multiply that number by "_ "
  38. # ex. 5 * "_ " -> "_ _ _ _ _ "
  39.  
  40. while True:
  41. sLetterGuessed = raw_input("Enter a letter or guess the word: ").lower()
  42. # initialize the letter the user is guessing and convert it to lower case
  43. # raw input will get the user's input
  44.  
  45. while True:
  46. if sLetterGuessed in sLettersUsed:
  47. # is the letter guessed a letter they have already guessed?
  48. # the code checks to see if sLetterGuessed is in the string sLettersUsed
  49. sLettersUsed += sLetterGuessed
  50. # add that later to the letters they have guessed
  51. # i.e. if the user guessed "s" and the letters they have guessed were "abc" the
  52. # new sLettersUsed will be "abcs"
  53. print "can't use that"
  54.  
  55. elif sLetterGuessed in sAnswer:
  56. # is the letter the user guessed in the answer word
  57. # i.e. is "o" in "word"
  58. sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
  59. # join (from the docs) returns a string that is a concatenation of the strings
  60. # in the iterable.
  61. # Essentially what this line is doing is replacing the "_" with the correct
  62. # letter where the correct letter is position. I'm assuming you have played
  63. # hangman before.
  64. sLettersUsed += sLetterGuessed
  65. print (sReveal)
  66. print HangingMan[iErrors]
  67. # not sure where HangingMan comes from. the purpose is to print the number of errors."
  68.  
  69. elif sLetterGuessed not in sAnswer:
  70. # if the user makes an incorrect guess.
  71. sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
  72. # same reveal function as above.
  73. sLettersUsed += sLetterGuessed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement