Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import string
- WORDLIST_FILENAME = "words.txt"
- def loadWords():
- """
- Returns a list of valid words. Words are strings of lowercase letters.
- Depending on the size of the word list, this function may
- take a while to finish.
- """
- print "Loading word list from file..."
- # inFile: file
- inFile = open(WORDLIST_FILENAME, 'r', 0)
- # line: string
- line = inFile.readline()
- # wordlist: list of strings
- wordlist = string.split(line)
- print " ", len(wordlist), "words loaded."
- return wordlist
- def chooseWord(wordlist):
- """
- wordlist (list): list of words (strings)
- Returns a word from wordlist at random
- """
- return random.choice(wordlist)
- # end of helper code
- # -----------------------------------
- # Load the list of words into the variable wordlist
- # so that it can be accessed from anywhere in the program
- wordlist = loadWords()
- print 'Welcome to the game, Hangman!'
- secret = random.choice(wordlist)
- print 'I am thinking of a word that is ' + str(len(secret)) + ' letters long.'
- guesses = ''
- turns = 8
- avLetters = string.ascii_lowercase
- while turns > 0:
- missed = 0
- for letter in secret:
- if letter in guesses:
- print letter,
- else:
- print '_',
- missed += 1
- print
- if missed == 0:
- print 'Congratulations, you won!'
- break
- print 'You have ' + str(turns) + ' guesses left.'
- guess = raw_input('Please guess a letter: ')
- guesses += guess
- if guess not in secret:
- turns -= 1
- if turns == 0:
- print 'Sorry, you ran out of guesses. The word was ', secret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement