Hangman

Python Hangman

Importing libraries

#Hangman
from time import sleep
import random
from hangman_parts import parts

Printing out a hangman picture

#Prints a hangman picture
print("Welcome to Hangman")
print('   ',  '------')
print('   ',  '|    |')
print('   ',  '|    O')
print('   ',  '|   -|-')
print('   ',  '|    |')
print('   ',  '|   / \\')
print('------------')

Let me think

print('Let me think of a word')

#Wait time function
def wait():
    for i in range(5):
        print('.', end = "")
        sleep(.5)
    print()
    
wait()

List of words

#List of words
words = ['mouse', 'cat', 'fish', 'robot', 'computer']

picked = random.choice(words)

print("Okay Ive got it!")

Number of letters

print("The word has", len(picked), "letters")

for i in range(len(picked)):
    print('_',' ', end = "")
    
print()

Right or wrong

#Creates a list with _ marks for letters
right = ['_'] * len(picked) 
wrong = []

Right letters

#Prints letters in right with _'s
def add_letter():
    for i in right:
        print(i, ' ', end = "")
    print()

Wrong letters

#Prints out wrong letters
def wrong_letter():
    print("Wrong letters:", end = "")
    for i in wrong:
        print(i,' ',end = "")
    print()

Main Loop

Guess a letter

#Main Loop        
while True:
    print('=====================')
    guess = input("Guess a letter")

If guess is correct

if guess in picked:
        print("Let me check")
        right[picked.index(guess)] = guess
        wait()
        add_letter()
        wrong_letter()
        parts(len(wrong))

If guess is wrong

elif guess not in picked:
        print("Let me check")
        wait()
        if guess in wrong:
            print("You already guessed", guess)
            wrong_letter()
        else:
            print(guess, "is not in my word")
            wrong.append(guess)
            add_letter()
            wrong_letter()
            parts(len(wrong))

Game over lose

if len(wrong) > 4:
        print("Game Over!")
        print("I picked", picked)
        break

Game over win

if '_' not in right:
        print("You guessed it!")
        print("I picked", picked)
        break

Main Loop Compete

#Main Loop        
while True:
    print('=====================')
    guess = input("Guess a letter")
    if guess in picked:
        print("Let me check")
        right[picked.index(guess)] = guess
        wait()
        add_letter()
        wrong_letter()
        parts(len(wrong))
        
    elif guess not in picked:
        print("Let me check")
        wait()
        if guess in wrong:
            print("You already guessed", guess)
            wrong_letter()
        else:
            print(guess, "is not in my word")
            wrong.append(guess)
            add_letter()
            wrong_letter()
            parts(len(wrong))
            
    if len(wrong) > 4:
        print("Game Over!")
        print("I picked", picked)
        break
    
    if '_' not in right:
        print("You guessed it!")
        print("I picked", picked)
        break

Final Code

hangman_parts

def parts(x):
    if x == 0:
        print('   ',  '------')
        print('   ',  '|    |')
        print('   ',  '|     ')
        print('   ',  '|     ')
        print('   ',  '|     ')
        print('   ',  '|     ')
        print('------------')
    if x == 1:
        print('   ',  '------')
        print('   ',  '|    |')
        print('   ',  '|    O')
        print('   ',  '|     ')
        print('   ',  '|     ')
        print('   ',  '|     ')
        print('------------')
    if x == 2:
        print('   ',  '------')
        print('   ',  '|    |')
        print('   ',  '|    O')
        print('   ',  '|   -|-')
        print('   ',  '|     ')
        print('   ',  '|     ')
        print('------------')
    if x == 3:
        print('   ',  '------')
        print('   ',  '|    |')
        print('   ',  '|    O')
        print('   ',  '|   -|-')
        print('   ',  '|    | ')
        print('   ',  '|     ')
        print('------------')
    if x == 4:
        print('   ',  '------')
        print('   ',  '|    |')
        print('   ',  '|    O')
        print('   ',  '|   -|-')
        print('   ',  '|    | ')
        print('   ',  '|   / \\')
        print('------------')

Main file

#Hangman
from time import sleep
import random
from hangman_parts import parts

#Prints a hangman picture
print("Welcome to Hangman")
print('   ',  '------')
print('   ',  '|    |')
print('   ',  '|    O')
print('   ',  '|   -|-')
print('   ',  '|    |')
print('   ',  '|   / \\')
print('------------')

print('Let me think of a word')

#Wait time function
def wait():
    for i in range(5):
        print('.', end = "")
        sleep(.5)
    print()
    
wait()

#List of words
words = ['mouse', 'cat', 'fish', 'robot', 'computer']

picked = random.choice(words)

print("Okay Ive got it!")

print("The word has", len(picked), "letters")

for i in range(len(picked)):
    print('_',' ', end = "")
    
print()


#Creates a list with _ marks for letters
right = ['_'] * len(picked) 
wrong = []

#Prints letters in right with _'s
def add_letter():
    for i in right:
        print(i, ' ', end = "")
    print()

#Prints out wrong letters
def wrong_letter():
    print("Wrong letters:", end = "")
    for i in wrong:
        print(i,' ',end = "")
    print()

#Main Loop        
while True:
    print('=====================')
    guess = input("Guess a letter")
    if guess in picked:
        print("Let me check")

        ###Handles letter that appear more than once
        
        index = 0
        for i in picked:
            if i == guess:
                right[index] = guess
            index = index + 1

        wait()
        add_letter()
        wrong_letter()
        parts(len(wrong))
        
    elif guess not in picked:
        print("Let me check")
        wait()
        if guess in wrong:
            print("You already guessed", guess)
            wrong_letter()
        else:
            print(guess, "is not in my word")
            wrong.append(guess)
            add_letter()
            wrong_letter()
            parts(len(wrong))
            
    if len(wrong) > 4:
        print("Game Over!")
        print("I picked", picked)
        break
    
    if '_' not in right:
        print("You guessed it!")
        print("I picked", picked)
        break