Advertisement
Guest User

Untitled

a guest
May 27th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. """
  2. This program plays a game of hangman.
  3. Guess letters in the mystery word.
  4. You can only make 8 incorrect guesses before you lose.
  5. See if you can guess the word before you run out of guesses...
  6.  
  7. Author: Xiang (Shawn) Pan
  8. October 28, 2013
  9.  
  10. """
  11. from random import*
  12. from string import*
  13.  
  14. def main():
  15. random_word=beginning()
  16. length=len(random_word)
  17. guess_number=[8]
  18. list_dash=intro(length)
  19. while_loop(random_word,list_dash,guess_number)
  20.  
  21.  
  22.  
  23. def beginning():
  24. """
  25. This function opens the text file and reads a word
  26. It has no input value and has two return values
  27. It returns the word randomly chosen and its length
  28. """
  29. word=open("words.txt",'r').readlines()
  30. word=word[randrange(len(word))]
  31. word=word.strip()
  32. return word
  33.  
  34. def intro(number):
  35. """
  36. This function has one input value and one return value
  37. It returns the list of dashes with the corresponding length to the word chosen
  38.  
  39. """
  40. all_dash="- "*number
  41. print all_dash
  42. print "incorrect guesses left: 8"
  43. return all_dash
  44.  
  45.  
  46. def test(all_inputs):
  47. """
  48. This function examines if the user's input is valid.
  49. This function has no input value from main function and has one return value.
  50. The user will enter the input in this step until the input is valid
  51.  
  52. """
  53. guess=raw_input("Enter a letter: ")
  54. while not guess.isalpha() or not len(guess)==1 or guess in all_inputs:
  55. if not guess.isalpha() or not len(guess)==1:
  56. print "invalid input"
  57. if guess in all_inputs:
  58. print "You already guessed '%s' , try again..." % (guess)
  59. guess=raw_input("Please reenter a letter: ")
  60.  
  61. return guess
  62.  
  63.  
  64. def check(word,input_user,list_dash,guess_number,list_input):
  65. """
  66. This function has 5 input values and 3 return values.
  67. It examines if the input value is a character in the word and change the list of dash corresponding to it
  68.  
  69. """
  70. #print word
  71. if input_user in word:
  72. print "good guess, %s is in the word." % (input_user)
  73. list_dash=yes(word,input_user,list_dash)
  74. else:
  75. print guess_number[0]
  76. number=guess_number[0]-1
  77. print "sorry there is no %s in the word." % (input_user)
  78. print "incorrect guesses left: %d" % (number)
  79. guess_number[0]=number
  80. print guess_number
  81. return list_dash
  82.  
  83.  
  84.  
  85.  
  86. def yes(word,guess_user,list_dash):
  87. """
  88. This function examines which dashes on the list will be replaced by the input character
  89. It has 3 input values and 1 return value
  90. It returns the list of dashes with characters filled in their corresponding space
  91.  
  92. """
  93. for n in range(len(word)):
  94. if guess_user==word[n]:
  95. new_dash=list(list_dash)
  96. new_dash[2*n]=guess_user
  97. list_dash="".join(new_dash)
  98. return list_dash
  99.  
  100.  
  101. def while_loop(random_word,list_dash,guess_number):
  102. """
  103. It is a while loop that has 3 input values and has no return value
  104.  
  105. """
  106. valid=True
  107. work=True
  108. list_input=""
  109. while valid and work:
  110. guess=test(list_input)
  111. list_input=list_input+guess
  112. list_dash=check(random_word,guess,list_dash,guess_number,list_input)
  113. print list_dash
  114. valid=check_number(guess_number)
  115. work=check_dash(list_dash)
  116.  
  117. def check_number(guess_number):
  118. """
  119. This function examines if the user has put input for 8 times.
  120. It has 1 input value and one return value
  121. """
  122.  
  123. if guess_number[0] <1:
  124. keep_going=False
  125. print "Sorry, you lost."
  126. else:
  127. keep_going=True
  128.  
  129. return keep_going
  130.  
  131. def check_dash(list_):
  132. """
  133. This function examines if all the dashes in the list of dashes have been filled by characters.
  134. It has one input value and one return value
  135. """
  136. list_=list(list_)
  137. if not "-" in list_:
  138. print "You won!!! Next time I'll win though"
  139. valid=False
  140. else:
  141. valid=True
  142. return valid
  143.  
  144.  
  145. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement