CLI Hangman - Python
2
A simple CLI hangman implementation in Python.
Uses a simple list of countries in countries.txt to generate words.
Uses a simple list of countries in countries.txt to generate words.
#Countries Hangman
#Import Modules
import random
import array
countries = open("countries.txt", "r") #Open countries list
wholefile = countries.read()
nolines= wholefile.count("\n") + 1 #Get number of lines in countries file
countries.seek(0) #Go back to start
randnum = random.randrange(nolines) #Choose random line number
i=0
while i<=randnum:
curline=countries.readline() #Convert random line number into random country
i+=1
word = curline.replace("\n", "") #remove carriage returns
obscured = array.array('c')
i=0 #new iteratior
while i < len(word):
obscured.append("-") #Create obscured array filled with -
i+=1
lives=5
done = []
solved = False
while not solved:
print "Word is: ", obscured.tostring()
print "Lives remaining: ", lives
curguess = raw_input("Enter guess: ") # get input
curguess = curguess.replace("\n", "") #remove carriage returns
if curguess not in done: #check for already guessed
if curguess in word: #check if char is present
i=0
lastval=0
while i < word.count(curguess):
obscured[word.index(curguess, lastval)]=curguess #replace some -
lastval = word.index(curguess, lastval) + 1
i+=1
elif curguess not in word:
print curguess, " is not in the word! \n" #not in word
lives-=1
if "-" not in obscured.tostring():
print "Word solved! \n Word was: ", word, ". Lives remaining: ", lives
solved = True #on solve
if lives == 0:
print "Game Over! Word was: ", word #out of lives
solved=True
done.append(curguess) #add curguess to already guessed
elif curguess in done:
print curguess, " already tried! \n" #if already done print so






There are currently no comments for this snippet.