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.

Dani AI

Generated

Solid, approachable first project that already captures the basic flow of a letter-guess game. A few focused changes will improve correctness, readability, and maintainability while keeping the program simple.

Recommended improvements (concise):

  • Use clear, consistent names (for example target, guessed_letters, wrong_letters) and follow PEP 8 for readability. This reduces the need for redundant comments.
  • Keep game state in small collections: use a set for letters already tried (fast membership, no duplicates) and separate sets for correct vs. wrong guesses.
  • Validate input: accept only a single alphabetic character for letter guesses, ignore or warn on empty input, and avoid counting repeated guesses as attempts.
  • Make the loop structure obvious: use a for loop over the allowed number of letter-guess attempts (or a while with a clear condition). Print the masked word and remaining attempts inside the loop so the player sees progress each turn.
  • Encapsulate behavior into functions (e.g., mask_word(target, guessed)) and use a main() with if __name__ == "__main__": for cleaner testing and reuse. This also makes unit testing straightforward.

Small, practical examples (not present in the thread):

masked = ''.join(ch if ch in guessed_letters else '_' for ch in target)
print(masked)
if letter in guessed_letters:
    print("Already tried")
else:
    guessed_letters.add(letter)
    (correct_letters if letter in target else wrong_letters).add(letter)

These changes address the mismatch between stated rules and loop behavior, avoid double-counting guesses, and make the program easier to extend (hint: adding a replay loop or difficulty levels becomes trivial). Builds on ’s naming and loop advice while keeping the original design and intent from intact.

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.