This is my first ever program created after reading a book on Python. Do you have any suggestions for me? Anything that are considered bad habits that I should correct for my new project?
#HangMan - 2014
import random
import time
#TODO: add word support
#TODO: add already guessed letters
secret = ""
dash = ""
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
def create_hangman():
create_hangman.guessess = create_hangman.guessess = 0
create_hangman.already_guessed = ""
#List of words, pick a word, then set it to a var
words = ["soccer", "summer", "windows", "lights", "nighttime", "desktop", "walk"]
d = random.randint(0, 6)
#Tell the compiler we want the global secret var
global secret
#Change the global secret v to a string while we choose the word
secret = str(words[d])
#The blank spaces. Find how many letters the word is and replace it with underscores
create_hangman.dash = ['_' for x in range(len(secret))]
#Print the hangman
print(HANGMANPICS[0], "\n",' '.join(create_hangman.dash))
def add_letter(letter):
create_hangman.already_guessessed = create_hangman.already_guessed, letter
def guess():
while True:
think = input("Pick a letter: ")
letter = think
alreadyGuessed = ""
if(len(letter) != 1):
print("Please enter only one letter.")
elif(letter not in 'abcdefghijklmnopqrstuvwxyz'):
print("Please guess a letter.")
elif(letter not in secret):
wrong_word(create_hangman.guessess)
add_letter(letter)
elif(letter in secret):
print("Congratulations!", letter, " was found!")
remove_dash(letter)
print_hangman()
check()
def wrong_word(hmpic):
create_hangman.guessess = create_hangman.guessess + 1
hmpic = create_hangman.guessess
if(create_hangman.guessess == 7):
you_loose()
else:
print(HANGMANPICS[hmpic], "\n", ' '.join(create_hangman.dash), "\n", "That letter is not in the word.")
def print_hangman():
print(HANGMANPICS[create_hangman.guessess] + "\n")
print(' '.join(create_hangman.dash))
def you_loose():
print("Sorry you lost! The correct word was", secret)
play_again = input("Would you like to play again: ");
if(play_again == "Y" or play_again == "y"):
create_hangman()
print("Creating a new game...")
elif(play_again == "N" or play_again == "n"):
print("Thanks for playing, bye!")
quit()
else:
print("Error: Please choose either 'Y' or 'N'")
return you_loose()
def you_win():
print("Congratulations! You won and got the word", secret)
play_again = input("Would you like to play again: ")
if(play_again == "Y" or play_again == "y"):
create_hangman()
print("Creating a new game...")
elif(play_again == "N" or play_again == "n"):
print("Thanks for playing, bye!")
quit()
else:
print("Error: Please choose either 'Y' or 'N'")
return you_loose()
def check():
if(''.join(create_hangman.dash) == secret):
you_win()
else:
guess()
def remove_dash(letter):
for i in range(len(secret)):
if secret[i] == letter:
create_hangman.dash = list(create_hangman.dash)
create_hangman.dash[i] = letter
name = input("Whats your name? ")
print("Hey", name, "welcome to HangMan 1.6")
create_hangman()
guess()