Advertisement
Guest User

Untitled

a guest
Oct 1st, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. # 6.00 Problem Set 3
  2. #
  3. # Hangman
  4. #
  5.  
  6.  
  7. # -----------------------------------
  8. # Helper code
  9. # (you don't need to understand this helper code)
  10. import random
  11. import string
  12.  
  13. WORDLIST_FILENAME = "words.txt"
  14.  
  15. def load_words():
  16.     """
  17.    Returns a list of valid words. Words are strings of lowercase letters.
  18.    
  19.    Depending on the size of the word list, this function may
  20.    take a while to finish.
  21.    """
  22.     print "Loading word list from file..."
  23.     # inFile: file
  24.     inFile = open(WORDLIST_FILENAME, 'r', 0)
  25.     # line: string
  26.     line = inFile.readline()
  27.     # wordlist: list of strings
  28.     wordlist = string.split(line)
  29.     print "  ", len(wordlist), "words loaded."
  30.     return wordlist
  31.  
  32. def choose_word(wordlist):
  33.     """
  34.    wordlist (list): list of words (strings)
  35.  
  36.    Returns a word from wordlist at random
  37.    """
  38.     return random.choice(wordlist)
  39.  
  40. # end of helper code
  41. # -----------------------------------
  42.  
  43. # actually load the dictionary of words and point to it with
  44. # the wordlist variable so that it can be accessed from anywhere
  45. # in the program
  46. wordlist = load_words()
  47.  
  48. # your code begins here!
  49. # issues: 1)updates the letters available. 2) displaying the word in a string vs. list
  50.  
  51. print "Welcome to the game, Hangman!"
  52. random_word = choose_word(wordlist)
  53. print random_word
  54. word = ''
  55.  
  56. for e in range (0, len(random_word)):
  57.     word = word + '-'
  58.  
  59. print word
  60.  
  61. string = 'abcdefghijklmnopqrstuvqxyz'
  62.    
  63. print "I am thinking of a word that is ", len(word), " letters long."
  64. print"--------------------"
  65. guesses = 8
  66.  
  67. while guesses > 0 and word != random_word:
  68.     print 'You have', guesses,  ' left.'
  69.     print "Available letter: ", string
  70.     attempt = raw_input("Please guess a letter: ")
  71.     for a in range (0, len(string)):
  72.         if attempt == string[a]:
  73.             string = string[0:a] + string[a+1:len(string)]
  74.             # Why do you need a break here????  
  75.     score = 0
  76.     for a in range (0, len(random_word)):
  77.         if random_word[a] == attempt:
  78.             word = word[0:a] + str(attempt) + word[a+1: len(word)]
  79.             score = score + 1
  80.     if score > 0:
  81.         print "Good guess:", word
  82.     else:
  83.         print "Oops! That letter is not in my word: ", word
  84.         guesses = guesses - 1
  85.     score = 0
  86.  
  87. if word == random_word:
  88.     print "You win", word
  89.    
  90. else:
  91.     print "Sorry champ not this time.", random_word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement