I'm new to programming (just about 2 months), and have been trying to learn as much as I can as quickly as I can.
I made a little Hangman game as a practice project this weekend. It doesn't have visuals — the user is allowed 5 strikes. I used the enable1.txt list of words.
Anyway, I was hoping to get some feedback on the game: specifically anything that can be improved or any major problems that I can notice now before they become ingrained bad habits.
import sys, time, random
# Initially sets 'word_list', 'word' (the secret word), and
# 'revealed' (the correct letters revealed to user) to None.
# These values will be set anew each time game starts
word_list = None
word = None
revealed = None
# Initially sets the number of strikes, hints, and
# letters already guessed to None. These values will be
# updated during game play
strikes = 0
hints = 0
already_guessed = []
def giveHint():
"""
Fills in one of the missing letters for the user.
"""
global hints
while True:
if hints < 3:
# Loops through letters in the secret word until it finds one
# the user hasn't already guessed
while True:
letter = random.choice(word)
if letter not in revealed:
hints += 1
print "\n HINT: '%s' " % letter
print " Hints used: %d/3 " % hints
return letter
break
else:
return None
break
break
def makeGuess():
"""
Provides Strikes and Hints status.
Then gets choice from user (a guess or a hint).
"""
global strikes, hints
# Provides game status to user
print '_' * 40
print
print ">>>>>>> STRIKES: %d/5 <<<<<<<" % strikes
print ">>>>>>> HINTS: %d/3 <<<<<<<" % hints
# Prints letters user has already guessed;
# set() used to only give a particular letter once,
# list() used to put those unique letters in a list,
# sorted() provides them in alphabetical order.
print "Already Guessed Letters: ", ' '.join(sorted(list(set(already_guessed))))
print "\nTHE BOARD:"
print ' '.join(revealed)
print
print "Guess a letter or the word"
print "To get a hint, enter '#hint':"
guess = raw_input("> ").lower()
# Creates blank space to distinguish turns in the game from eachother
print "\n" * 5
print "_" * 40
print
# If user asks for a hint, assign result of giveHint() to guess variable
if '#hint' in guess:
guess = giveHint()
return guess
def revealWord(guess=None):
"""
Prints out the secret word letter by letter
"""
print "THE SECRET WORD IS:",
for i in range(6):
sys.stdout.write('. ',)
sys.stdout.flush()
time.sleep(.3)
for letter in word:
sys.stdout.write(letter,)
sys.stdout.flush()
time.sleep(.3)
print
return
def hangman():
global strikes, hints, already_guessed, revealed, word
# As long as there is a blank space in the "revealed" list,
# the user continues to guess letters, unless strikes > 5
# or the user guesses the whole word
while '_' in revealed and strikes < 5:
guess = makeGuess()
if strikes == 5:
break
# guess == none when giveHint() returns None
# no break allows the while loop to continue
elif guess == None:
print "SORRY %d/3 hints used" % hints
# If the user guesses the whole wordcorrectly,
# 'revealed' variable set to 'word', which allows
# the winning outcome condition to be met.
elif len(guess) > 1:
print "You guessed '%s'\n" % (''.join(guess))
if guess == word:
revealed = word
break
elif guess in already_guessed:
print "You've already guessed that letter!"
elif guess in word:
print "Nice! '%s' is in the word" % guess
for index, letter in enumerate(word):
if guess == letter:
revealed[index] = guess
already_guessed.append(guess)
elif guess not in word and strikes < 5:
print "Sorry '%s' not found!...." % guess
strikes += 1
already_guessed.append(guess)
else:
return
break
# Outcomes:
# -------------------------------------------------------
# Losing outcome if strikes >= 5
if strikes >= 5:
print "*" * 10, " STRIKE 5! ", "*" * 10
print "*" * 10, " SORRY, YOU LOSE. ", "*" * 10
revealWord()
# resets strikes, hints, and already_guessed list
strikes, hints, already_guessed = 0, 0, []
# returns to starting menu
start()
return
# Winning outcome if no empty slots left for guessing
elif '_' not in revealed:
revealWord()
print "*" * 10, " CONGRATS, Ya Got It! ", "*" * 10
# resets strikes, hints, and already_guessed list
strikes, hints, already_guessed = 0, 0, []
# returns to starting menu
start()
return
# Losing outcome if the wrong word was guessed
else:
print "Sorry....You Lose."
revealWord(guess)
# resets strikes, hints, and already_guessed list
strikes, hints, already_guessed = 0, 0, []
# returns to starting menu
start()
return
def start():
global word_list, word, revealed
# opens enable1.txt file and assign random word to variable 'word'
word_list = open("enable1.txt", 'r').readlines()
word = random.choice(word_list).replace('\n','').replace('\r','')
# sets 'revealed' variable to '_'s the length of the chosen random word
revealed = ['_' for i in range(len(word))]
# Gives user choice to play or quit
print
choice = raw_input("Press 'Y' to Play\nOr 'Q' to Quit: ")
if choice.lower() == "y":
hangman()
else:
print "Goodbye!"
sys.exit()
print
print "Welcome to HANGMAN."
print "-------------------"
start()