patternpythonMinor
Hangman game in Python
Viewed 0 times
gamepythonhangman
Problem
While I'm not new to Python, this is my first 'bigger' Python program I created, also my first game in Python.
```
import random
DEBUG = False; # If set to true, displays chosen word before each game. For debugging and cheating only :)
DICT_FILE = "english.txt";
GUESSES = 10;
def prompt(s):
while True:
ans = raw_input(s + " (Y/n) ").lower();
if ans in ["y", "n"]:
return ans == "y";
def loadWords():
with open(DICT_FILE, "r") as f:
return f.read().strip().split("\n");
# Strip in case of newline at end of file
def play(word):
word = word.lower();
if DEBUG:
print "[DEBUG] word is %r" % (word);
guesses = GUESSES;
toGuess = set(l for l in word.upper() if l.isalpha()); # Handles apostrophes, etc.
letters = set();
while guesses > 0:
if len(toGuess) == 0:
print "You win with %d guess%s left!" % (guesses, "" if guesses == 1 else "es");
print "The word is %r" % (word);
return True;
print "=" * 78;
print "You have %d guess%s left." % (guesses, "" if guesses == 1 else "es");
print "Word: " + " ".join("_" if c in toGuess else c for c in word.upper());
print "Letters: " + " ".join(sorted(letters));
while True:
l = raw_input("Choose a letter: ").upper();
if l.isalpha():
if l not in letters:
letters.add(l);
if l in toGuess:
toGuess.remove(l);
print "Correct!";
else:
guesses -= 1;
print "Sorry, not in the word.";
break;
else:
print "You already chose that letter!";
else:
print "That's not a letter!";
print "=" * 78;
print "Sorry, you lose.";
print "The word was: %r" % (word);
return False;
if __name__
```
import random
DEBUG = False; # If set to true, displays chosen word before each game. For debugging and cheating only :)
DICT_FILE = "english.txt";
GUESSES = 10;
def prompt(s):
while True:
ans = raw_input(s + " (Y/n) ").lower();
if ans in ["y", "n"]:
return ans == "y";
def loadWords():
with open(DICT_FILE, "r") as f:
return f.read().strip().split("\n");
# Strip in case of newline at end of file
def play(word):
word = word.lower();
if DEBUG:
print "[DEBUG] word is %r" % (word);
guesses = GUESSES;
toGuess = set(l for l in word.upper() if l.isalpha()); # Handles apostrophes, etc.
letters = set();
while guesses > 0:
if len(toGuess) == 0:
print "You win with %d guess%s left!" % (guesses, "" if guesses == 1 else "es");
print "The word is %r" % (word);
return True;
print "=" * 78;
print "You have %d guess%s left." % (guesses, "" if guesses == 1 else "es");
print "Word: " + " ".join("_" if c in toGuess else c for c in word.upper());
print "Letters: " + " ".join(sorted(letters));
while True:
l = raw_input("Choose a letter: ").upper();
if l.isalpha():
if l not in letters:
letters.add(l);
if l in toGuess:
toGuess.remove(l);
print "Correct!";
else:
guesses -= 1;
print "Sorry, not in the word.";
break;
else:
print "You already chose that letter!";
else:
print "That's not a letter!";
print "=" * 78;
print "Sorry, you lose.";
print "The word was: %r" % (word);
return False;
if __name__
Solution
This is Python and you do not need the Semi-Colons (
Your code looks absolutely non-pythonic like that, it looks like it is trying to morph into Java or something.
You should also separate some more code into methods/functions like this one here
I believe that this is to separate the game from the ending notifications.
What gave this away was
;)Your code looks absolutely non-pythonic like that, it looks like it is trying to morph into Java or something.
You should also separate some more code into methods/functions like this one here
print "=" * 78;I believe that this is to separate the game from the ending notifications.
What gave this away was
78 I have no clue what that is, and if I have different settings on my console/IDE then this won't look right anyway. 78 is a magic number and should be dealt with so that I know what it meansCode Snippets
print "=" * 78;Context
StackExchange Code Review Q#74503, answer score: 6
Revisions (0)
No revisions yet.