Hangman v1.0

posted Feb 25, 2016, 7:56 AM by Attila Lengyel
def counter(sWord, sChar):
    iCount = 0
    for x in range (0,len(sWord)):
        if sWord[x] == sChar:
            iCount += 1
    return iCount

def drawHangman(missedGuess):
    if missedGuess > 0:
        print("       _________")
    else:
        print("")
    if missedGuess > 1:
        print("       |        |")
    else: 
        print("       |        ")
    if missedGuess > 2:
        print("       |        0")
    else: 
        print("       |        ")
    if missedGuess > 3:
        print("       |       /|\\")    
    else: 
        print("       |        ")
    if missedGuess > 4:
        print("       |        |")
    else: 
        print("       |        ")
        
    if missedGuess > 5:
        print("       |       / \\")
    else: 
        print("       |        ")
    
    print("       _________")
    print("")

    if missedGuess > 6:
        return True
    else:
        return False
    
def printWord(sWord, sCorrectLetter):
    returnString = ""
    iShownLetters = 0
    
    for x in range(0, len(sWord)):
        if counter(sCorrectLetter, sWord[x]) == 0:
            returnString += "*"
        else:
            returnString += sWord[x]
            iShownLetters += 1
    
    print(returnString)

    if iShownLetters == len(sWord):
        return True
    else:
        return False
    
print("This will be my hangman game")

iMissed = 0
sCorrectLetter = ""

import getpass
sWord = getpass.getpass("Give me a word:")
sName = input("What is your first name?:")
print("Hello ",sName, "How are you doing? Today you have to fight for your life!")
sGuess = ""
while sGuess != "stop":
    sGuess = input("Guess a letter:")
    
    if counter(sWord, sGuess[0]) == 0:
        iMissed += 1
        print("Sorry,",sName," You have missed.")
    else:
        sCorrectLetter += sGuess[0]
        print("Excellent! ", sName, " It was a good guess!")
    
    if drawHangman(iMissed):
        print(sName, "You lost! Try again!")
        break
    
    if printWord(sWord, sCorrectLetter):
        print(sName, " Congratulation!!! You have won!")
        break
    
    
Comments