Hi, I am starting out with python coding and was looking for some feedback.
Constructive Criticism please.

Here is my code for game "Word Guess".

# Word Guess
#
# Word guess, is a game where the user has five chances to ask
# the computer if a certain letter is in the word chosen.
# then the player must make a guess at the whole word.

# Must import random to gather a random word from tuple
import random

WORDS = ("python",
         "dictionary",
         "happy",
         "help",
         "easy",
         "language")

wordTuple = random.choice(WORDS)

# Create variable for correct.
correct = wordTuple

# Explain the game
print(
     """
              Word Guessing.
                 Written:
                 Sarcasm
        Choose up to five letters
      then make a guess at the word.
     (press enter at prompt to exit.)

     """
     )

# Create the guessing of letters.
lGuess = input("Please guess a letter: ")
gRight = ""
gWrong = ""
numGuess = 0

# Set up number of tries
while numGuess <= 3:
    if lGuess.lower() not in correct:
        gWrong += lGuess.lower()
        lGuess = input("No, Try again: ")
        numGuess += 1

    else:
        gRight += lGuess.lower()
        lGuess = input("Yes, Enter another: ")
        numGuess += 1

# Start the guessing for the word.
print("\nNow you must guess the word, you have one try.")

# Print the Letters in and not in.
print("\nLetters not in the word:",gWrong)
print("\nLetters in the word:",gRight)

# Get the user guess for word.
wGuess = input("\nYour Guess: ")

while True:
    if wGuess.lower() == correct:
        print("\nWOW! You guessed correct.")
        break
    
    else:
        print("\nSorry, Better luck next time.")
        break

# Thank the player!!!
print("\nThanks for playing!")

# wait for user to exit
input("\n\nPress the enter key to exit.")

Oh and please be gentle written with no additional help.

Recommended Answers

All 2 Replies

Because Python has 2.x and 3.x versions, it is wise to mention the python version in the module comment or string

It would be nice of you to print a summary of the state of the game in the game loop. Such as print("Correct guesses: %s; Wrong guesses: %s"%(correct_guesses, wrong_guesses)) To avoid having duplicate code to accept the user's guess, I usually use a loop like this

prompt = "Guess a letter " # note the trailing space
while True:
  # display
  print(whatever)
  # ask
  answer = input(prompt)
  # analyze
  # 
  if [I]end_condition[/I]:
     break
  # get ready for next loop, for instance:
  prompt="correct prompt for this loop"

Your line 64 is redundant. Just the if/else is enough.

Usually, you don't require the user to end the game unless the "leave the game" condition is inside the game loop. Or unless the game is running inside a window that will close when the code stops.

the variable name wordTuple is misleading because it is not a tuple: word_from_tuple might make sense, but I would prefer to_guess , or target (I don't expect perfect English). Every variable should be a good name that you never have to think about how it is spelled or what it means. Standard Python naming prefers that classes be named in CamelCase; and that variables and functions should have words_separated_by_underbars. Note that good variable names are much better than comments for self-documenting code: The variable name is seen everywhere it is used, but the comment only where declared.

Some of your comments are also redundant. For instance "# Must import random to gather a random word from tuple". There is no reason to comment about why you need to use the module random. We know what is in it. Lines 20 and 42 are also a bit more than needed.

My thumb-rule for comments:

  • If it is "tricky" make a comment
  • If it documents what the code does for a maintainer, make a comment
  • If the code has blocks that do a unit of work, maybe comment at the top of the block
  • If there is complex nested logic, use comments to keep track of which logic element ends where
  • Be sure that every function has a useful docstring which is better than a comment because there are automatic tools to create documentation from such strings

Thank you very much for you feedback.

I will use this in future python programming, this python version is 3.2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.