Tuesday, May 6, 2014

Fun with Python: Hangman Game

import random
#We should use the random library to choose a random word.
#Add "import random" to the top of your program
#Change the line that sets up the secret so that it looks like below
#The square brackets [ ] and commas make a list
#And the random.choice picks one thing randomly from the list.

#If you need to load a List from the Internet, follow the three steps below
#import urllib.request
#animals = urllib.request.urlopen('http://davidbau.com/data/animals') .read() .decode("utf8") .split()
#secret = random.choice(animals)


print("LET'S PLAY HANGMAN, BITCHES. OR DIE!!")
secret = random.choice(['elephant', 'lion', 'horse', 'mouse', 'tiger', 'panda', 'turtle','beaver'])
guesses = 'aeiou'
turns = 7

while turns > 0:
    missed = 0
    for letter in secret:
        if letter in guesses:
            print(letter, end=" ")
        else:
            print('_', end=" ")
            missed +=1

           
#You need to indent everything under the "while" command to make this work.
#So you will need to add some spaces in front of most of your program.

#Let's also move the guessing after the hint instead of before.

#The command "turns -= 1" means subtract one from "turns," so if it used to be 5, it will be 4.
#Then the next time around it will be 3 and so on. When turns is finally zero, the "while" command will stop repeating.

    print

    if missed == 0:
#The "missed" number counts how many blanks still missing. If it is zero, it means we won.
        print("YOU WIN, DUMBASS!!")
        break #And the "break" command breaks out of the "while" section early

    guess = input("GUESS A LETTER, YOU MORON: ")
#To let the player guess we will use a function called "input()"
    guesses += guess

    #The 'guesses += guess' line adds the guess to the string of guesses.
   #If the string of guesses was "aeiou" and the new guess is "c", then the string of guesses will become "aeiouc"

    if guess not in secret:
        turns -=1
        print("NOOOOOOOOO!!")
        print(turns, "MORE TURNS")
        if turns < 7: print("   0   ")
        if turns < 6: print("   |   ")
        if turns < 5: print(" \_|_/ ")
        if turns < 4: print("   |   ")
        if turns < 3: print("   |   ")
        if turns < 2: print("  / \  ")
        if turns < 1: print(" d   b ")
        if turns == 0:
            print("YOU ARE AN IDIOT! THE ANSWER IS", secret)
#The "if guess not in secret" line checks if the guess was wrong.
#We only count down the "turns" if our guess was wrong
#When we guess wrong, we also print a bunch of stuff like "Nooooooo!!"
#Along with how many more turns we have.
#When we are wrong for the last time, we print the secret.