Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  • removing the semicolons, as Malachi Malachi and Josay Josay pointed out,
  • formatting the comments to follow the pep8 guide,
  • Changing print "=" * 78 to print "===", a three character wide separator is enough, while removing magic numbers. The separator will now look good in any console/terminal, (As pointed out by Malachi Malachi), I don't think creating a new function is worth it, because this is a simple statement used only twice.
  • Renamed all word-related variables to phrase, the game can handle any phrase, not just a single word.
  • As Mark Mark and Josay Josay pointed out, my play() function is a bit too long. I broke it up a bit.
  • Mark Mark pointed out, that I needlessly convert my phrase to lowercase, only to convert it to uppercase later. I changed it to be stored as uppercase.
  • removing the semicolons, as Malachi and Josay pointed out,
  • formatting the comments to follow the pep8 guide,
  • Changing print "=" * 78 to print "===", a three character wide separator is enough, while removing magic numbers. The separator will now look good in any console/terminal, (As pointed out by Malachi), I don't think creating a new function is worth it, because this is a simple statement used only twice.
  • Renamed all word-related variables to phrase, the game can handle any phrase, not just a single word.
  • As Mark and Josay pointed out, my play() function is a bit too long. I broke it up a bit.
  • Mark pointed out, that I needlessly convert my phrase to lowercase, only to convert it to uppercase later. I changed it to be stored as uppercase.
  • removing the semicolons, as Malachi and Josay pointed out,
  • formatting the comments to follow the pep8 guide,
  • Changing print "=" * 78 to print "===", a three character wide separator is enough, while removing magic numbers. The separator will now look good in any console/terminal, (As pointed out by Malachi), I don't think creating a new function is worth it, because this is a simple statement used only twice.
  • Renamed all word-related variables to phrase, the game can handle any phrase, not just a single word.
  • As Mark and Josay pointed out, my play() function is a bit too long. I broke it up a bit.
  • Mark pointed out, that I needlessly convert my phrase to lowercase, only to convert it to uppercase later. I changed it to be stored as uppercase.
Source Link
Mateon1
  • 235
  • 2
  • 10

From what I've seen answered so far, my main issue is using semicolons after every statement and not following pep8. I've fixed this in my code by:

  • removing the semicolons, as Malachi and Josay pointed out,
  • formatting the comments to follow the pep8 guide,
  • Changing print "=" * 78 to print "===", a three character wide separator is enough, while removing magic numbers. The separator will now look good in any console/terminal, (As pointed out by Malachi), I don't think creating a new function is worth it, because this is a simple statement used only twice.
  • Renamed all word-related variables to phrase, the game can handle any phrase, not just a single word.
  • As Mark and Josay pointed out, my play() function is a bit too long. I broke it up a bit.
  • Mark pointed out, that I needlessly convert my phrase to lowercase, only to convert it to uppercase later. I changed it to be stored as uppercase.

Here's my final code:

import random
# If set to true, displays chosen word before each game.
# For debugging and cheating only :)
DEBUG = False
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 read_phrases():
 with open(DICT_FILE, "r") as f:
 return f.read().strip().split("\n")
 # Strip before splitting in case of newline at end of file
def prompt_letter(letters_so_far):
 while True:
 c = raw_input("Choose a letter: ").upper()
 if c.isalpha():
 if c not in letters_so_far:
 return c
 else:
 print "You already chose that letter!"
 else:
 print "That's not a letter!"
def play(phrase):
 phrase = phrase.upper()
 if DEBUG:
 print "[DEBUG] phrase is %r" % (word)
 guesses = GUESSES
 to_guess = set(l for l in phrase if l.isalpha()) # Handles apostrophes, etc.
 letters = set()
 while guesses > 0:
 if len(to_guess) == 0:
 print "You win with %d guess%s left!" % (guesses, "" if guesses == 1 else "es")
 print "The phrase is %r" % (phrase)
 return True
 print "==="
 print "You have %d guess%s left." % (guesses, "" if guesses == 1 else "es")
 print "Phrase: " + " ".join("_" if c in to_guess else c for c in phrase)
 print "Letters: " + " ".join(sorted(letters))
 while True:
 c = prompt_letter(letters)
 letters.add(c)
 if c in to_guess:
 to_guess.remove(c)
 print "Correct!"
 else:
 guesses -= 1
 print "Sorry, not in the phrase."
 break
 print "==="
 print "Sorry, you lose."
 print "The phrase was: %r" % (phrase)
 return False
if __name__ == "__main__":
 phrases = read_phrases()
 random.shuffle(phrases)
 while True:
 play(phrases.pop()) # Remove a random phrase so it's not chosen again
 if not prompt("Would you like to play again?"):
 break
lang-py

AltStyle によって変換されたページ (->オリジナル) /