I know that some features overlap but this is because I added different functions to the code at different stages when I thought of them.
Note: The code works on both Python 3 and 2.
import random,sys
Hangman = ['''
+---+
|
|
|
|
|
=========''','''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split()
def menu():
pass
def cpuPlays():
pass
def GetRandomWord(word):
chosenWord = random.choice(word)
return chosenWord
def wordLength(word,wordList):
userEntury = input("Would you like Tier 1 or Tier 2 words? (1 or 2):")
if userEntury == '1':
index = wordList.index(word)
index-=1
if len(word) >= 5:
#print(word,'1')
return word
else:
for n in range(len(wordList)):
index+=1
if len(wordList[index]) >= 5:
word = wordList[index]
#print(word,'2')
return word
elif len(wordList) == index:
index = len(wordList) - index
while len(wordList[index])<5:
index +=1
if len(wordList[index]) >= 5:
word = wordList[index]
#print(word,'3')
return word
elif userEntury == '2':
index = wordList.index(word)
index-=1
if len(word) <= 4:
#print(word,'1')
return word
else:
for n in range(len(wordList)):
index+=1
if len(wordList[index]) <= 4:
word = wordList[index]
#print(word,'2')
return word
elif len(wordList) == index:
index = len(wordList) - index
while len(wordList[index])>4:
index +=1
if len(wordList[index]) <=4:
word = wordList[index]
#print(word,'3')
return word
def display(hangmanPic,secretWord,numWrongLetters,correctLetters):
blanks = ['-']*len(secretWord) # makes list of strings instead of putting all into one string
for i in range(len(secretWord)):#repleaces blank letters with correct letters
if secretWord[i] in correctLetters:
blanks[i] = secretWord[i] #looks through each string and changes it if needed
print("Missing Letters:")
for letter in blanks:
print(letter,end='')
print(hangmanPic[numWrongLetters])
def getGuess(alreadyGuessed):
while True:
print("Guess Letter:")
guess = input()
guess = guess.lower()
if len(guess) != 1:
print("Please enter only 1 letter.")
elif guess in alreadyGuessed:
print("Letter is already guessed.")
elif guess.isdigit():
print("Please enter a letter not integer.")
else:
return guess
def playAgain():
print("Do you want to play again?(yes or no)")
return input().lower().startswith('y')
print("H A N G M A N")
correctLetters = ''
guessedLetters = ''
wrongLetters = 0
randomWord = GetRandomWord(words)
#print(randomWord)
gameDone = False
GameIsRunning = True
WordLength = wordLength(randomWord,words)
while GameIsRunning:
display(Hangman,WordLength,wrongLetters,correctLetters)
guess = getGuess(correctLetters + guessedLetters)
if guess in randomWord:
correctLetters += guess
#Checks if player has won
foundAllLetters = True
for i in range(len(randomWord)):
if randomWord[i] not in correctLetters:
foundAllLetters = False
break
if randomWord[i] in correctLetters:
foundAllLetters = True
print("Well Done You found what the missing word is!")
gameDone = True
else:
wrongLetters +=1
guessedLetters += guess
#Check if player has lost
if wrongLetters == len(Hangman)-1:
print(Hangman[7])
print("""You have ran out of guesses the word was %s. You had %d correct guess(es) out of %d in total.
""" % (randomWord,len(correctLetters),len(Hangman)))
gameDone = True
#Ask player to play again
if gameDone == True:
if playAgain():
wrongLetters = 0
guessedLetters = ''
correctLetters = ''
randomWord = GetRandomWord(words)
WordLength = wordLength(randomWord,words)
gameDone = False
else:
GameIsRunning = False
exit()