4
\$\begingroup\$

This is the first project I am attempting in Python, and my first time on Stack Exchange, so please be kind.

The game uses a text file with lots of words. I was wondering how to improve the game by having different difficulty levels. E.g. easy for 1-4 letters, medium for 5-8 letters, hard for 9+ letters in the word. All the code in print_graphics() was pre-made, so I have to figure out a way to incorporate it into the code I was writing.

I was also thinking it might look neater if it didn't keep printing new iterations of the hangman after each body part is added, but I'm not sure how I could do that without massively changing the text.

I couldn't figure out how to attach the input file (user interaction) so I've linked it instead.

import random
from IPython.display import clear_output
from termcolor import colored

def print_graphics(wrong_guesses):
    # list of possible body parts
    body_parts = ['  O     |', '  |     |',' /|     |', ' /|\    |', ' /      |', ' / \    |']
    
    # how many lines to print
    lines = 4 if wrong_guesses != 0 else 5

    # check number provided is usable
    if 0 <= wrong_guesses <= 6:
        print('  +-----+')  # print top of frame
        print('  |     |')
    
        # print the correct body parts for current state
        if wrong_guesses > 0:
            if wrong_guesses > 1:
                print(body_parts[0])
                lines -= 1
            if wrong_guesses > 4:
                print(body_parts[3])
                lines -= 1
            print(body_parts[wrong_guesses-1])

        for i in range(lines):
            print('        |')  # print the lines
        
        print('==========')  # print the floor


with open("wordlist.txt") as file:
  words = file.read().split()
  word = random.choice(words)

guessed_word = []
guessed_letters = []
max_guesses = 5
length_word = len(word)
alphabet = "abcdefghijklmnopqrstuvwxyz"
letter_storage = []
body_parts = ['  O     |', '  |     |',' /|     |', ' /|\    |', ' /      |', ' / \    |']


def start():
  print("\033[1;34m"+"Welcome to hangman! You have 6 tries to guess the correct word, enter a letter to get started", "\033[0;30m")
start()

def intro():
  for character in word:
    guessed_word.append("_")
  print("The word you are guessing has", length_word, "characters")
  print_graphics(0)
intro()

def play(word):
  wrong_guesses = 0
  while wrong_guesses <6:
    guess = input("Guess a letter or word: ")

    if guess in guessed_letters:
      print("\033[1;31m"+"Whoops! You have already guessed this letter. Don't worry, you still have", 6-wrong_guesses, "guesses remaining" "\033[0;30m")
    if guess not in alphabet:
      print("\033[1;31m"+"Character invalid. Please enter a letter", "\033[0;30m")
    
    elif guess in alphabet and guess not in guessed_letters:
      if guess in word: 
        print("\033[1;3;32m"+"Well done", guess.upper(), "is correct!", "\033[0;30m")
        guessed_letters.append(guess)
        for i in range(0, length_word):
          if word[i] == guess:
            guessed_word[i] = guess
        print(guessed_word)
        if not "_" in guessed_word:
          print("\033[1;3;32m"+"Congratulations, you beat the Hangman!", "\033[0;30m")
          break 

      elif guess not in word:
        wrong_guesses += 1
        print(print_graphics(wrong_guesses))
        print(guessed_word)
        print("\033[1;31m"+guess.upper(), "is not correct. You have", 6-wrong_guesses, "guesses left", "\033[0;30m")
        guessed_letters.append(guess)    
    if wrong_guesses == 6:
      print("\033[1;31m"+"Bad luck! You have been beaten by the Hangman. The word you were trying to guess was", word, "\033[0;30m")
  



play(word)
print("\033[1;3;35m"+"Game over, thank you for playing :)")
\$\endgroup\$
3
  • 4
    \$\begingroup\$ On Code Review, we can offer advice on improving the existing code. Changes to the functionality (adding difficulty levels) are off-topic, so don't expect reviewers to add that for you! \$\endgroup\$ Dec 30, 2020 at 11:19
  • 1
    \$\begingroup\$ I wasn't looking for anyone to do the code for me as I am trying to learn! I was simply hoping someone could review the code and suggest how to read the number of characters in the words in the file. so far I have: def difficulty(word): print("Easy = word has 2-4 characters\nMedium = word has 5-8 characters\nHard = word has 9+ characters") diff = input("Do you want easy, medium or hard?") if diff is easy: len(word) >= 2 and <= 4 if diff is medium: len(word) >= 5 and <= 8 if diff is hard: len(word) >9 difficulty(word) but there is nothing about the file \$\endgroup\$
    – JessicaBr
    Dec 30, 2020 at 14:35
  • 1
    \$\begingroup\$ Code review is not a site to ask for new features, we only review existing features and provide insight on how it might be improved. \$\endgroup\$ Dec 30, 2020 at 18:46

1 Answer 1

2
\$\begingroup\$

You write

from termcolor import colored

but then ignore that and write non-portable escape sequences directly in the strings:

  print("\033[1;34m"+"....", "\033[0;30m")

Doesn't that defeat the point of termcolor, which is supposed to adapt to the connected terminal?

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for pointing that out, I tried to it that way originally but it wasn't working and I forgot to remove that module \$\endgroup\$
    – JessicaBr
    Dec 30, 2020 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.