Python Hangman Game Source code


SUBMITTED BY: alemotta

DATE: April 4, 2017, 1:14 p.m.

FORMAT: Text only

SIZE: 1.8 kB

HITS: 657

  1. Python Hangman Game Source code
  2. #importing the time module
  3. import time
  4. #welcoming the user
  5. name = raw_input("What is your name? ")
  6. print "Hello, " + name, "Time to play hangman!"
  7. print "
  8. "
  9. #wait for 1 second
  10. time.sleep(1)
  11. print "Start guessing..."
  12. time.sleep(0.5)
  13. #here we set the secret
  14. word = "secret"
  15. #creates an variable with an empty value
  16. guesses = ''
  17. #determine the number of turns
  18. turns = 10
  19. # Create a while loop
  20. #check if the turns are more than zero
  21. while turns > 0:
  22. # make a counter that starts with zero
  23. failed = 0
  24. # for every character in secret_word
  25. for char in word:
  26. # see if the character is in the players guess
  27. if char in guesses:
  28. # print then out the character
  29. print char,
  30. else:
  31. # if not found, print a dash
  32. print "_",
  33. # and increase the failed counter with one
  34. failed += 1
  35. # if failed is equal to zero
  36. # print You Won
  37. if failed == 0:
  38. print "
  39. You won"
  40. # exit the script
  41. break
  42. print
  43. # ask the user go guess a character
  44. guess = raw_input("guess a character:")
  45. # set the players guess to guesses
  46. guesses += guess
  47. # if the guess is not found in the secret word
  48. if guess not in word:
  49. # turns counter decreases with 1 (now 9)
  50. turns -= 1
  51. # print wrong
  52. print "Wrong
  53. "
  54. # how many turns are left
  55. print "You have", + turns, 'more guesses'
  56. # if the turns are equal to zero
  57. if turns == 0:
  58. # print "You Loose"
  59. print "You Loose"

comments powered by Disqus