patternpythonMinor
Beginner Hangman in Python
Viewed 0 times
beginnerpythonhangman
Problem
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 >>>>>> STRIKES: %d/5 >>>>>> HINTS: %d/3 ").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 stri
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 >>>>>> STRIKES: %d/5 >>>>>> HINTS: %d/3 ").lower()
# Creates blank space to distinguish turns in the game from eachother
print "\n" * 5
print "_" * 40
# 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)
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 stri
Solution
Nice code, really enjoyed reading this. Well-documented and understandable.
Minor comments:
Read about python modules to make your code re-usable.
Minor comments:
- Use this construct to start program - this will make your code re-usable as you will be able to run this file by itself and also include into other module:
if __name__ == "__main__":
start()- Avoid creating global variables - you can easily create a class to contain all variables required for you and also all the methods of the game. If you will be creating mult-module program - this will help to avoid global mames conflics and is generally cleaner.
Read about python modules to make your code re-usable.
Code Snippets
if __name__ == "__main__":
start()Context
StackExchange Code Review Q#108702, answer score: 2
Revisions (0)
No revisions yet.