Skip to content

Instantly share code, notes, and snippets.

@AnastasiaDunbar
Created May 25, 2016 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnastasiaDunbar/aefae8de8bf8bb2e26e83a2402df3544 to your computer and use it in GitHub Desktop.
Save AnastasiaDunbar/aefae8de8bf8bb2e26e83a2402df3544 to your computer and use it in GitHub Desktop.
May be badly coded
#Hangman
import requests
word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = requests.get(word_site)
words = response.content.splitlines()
import random
import re
def randomWord():
return re.sub('(!?[^^A-z])','',random.choice(words).lower())
def createArray(size,value):
a = []
for i in range(size):
a += [value]
return a
wordwas = "Word was actually "
while(True): #Endless mode
word = randomWord()
show = createArray(len(word),False)
letters_guessed = []
lives = 10
while(lives>0):
print("Lives: "+str(lives))
s = ''
for i in range(len(word)):
s += (word[i] if show[i] else '_')+' '
print(s)
print("Letters guessed: "+(','.join(letters_guessed)))
while(True):
guessLetter = str(raw_input('Enter a letter: ')).lower()
if (len(guessLetter)>1):
print("Too long!")
elif (len(guessLetter)==0):
print("You must enter!")
elif (guessLetter in letters_guessed):
print("You've already entered this letter!")
elif (not guessLetter.isalpha()):
print('That "letter" is not included.')
else:
letters_guessed.append(guessLetter)
break
if (guessLetter in word):
for i in range(len(word)):
if (word[i]==guessLetter):
show[i] = True;
if (show.count(True)==len(word)):
print('\nYou won! '+wordwas+'"'+word+'".\n')
break #End game
else:
if (lives-1<=0):
print('\nGame over! '+wordwas+'"'+word+'".\n')
break #End game
else:
print("WRONG!")
lives-=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment