Hangman v4

posted Feb 11, 2016, 8:03 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 = ""
sWord = input("Give me a word:")
sGuess = ""
while sGuess != "stop":
    sGuess = input("Guess a letter:")
    
    if counter(sWord, sGuess[0]) == 0:
        iMissed += 1
    else:
        sCorrectLetter += sGuess[0]
    
    if drawHangman(iMissed):
        print("You lost! Try again!")
        break
    
    if printWord(sWord, sCorrectLetter):
        print("Congratulation!!! You have won!")
        break
    
    
    

Comments