A BASIC Hangman Game

jib 0 Tallied Votes 960 Views Share

###############################
# PROLOG SECTION
#
# Program to play the traditional game of hangman.
# The computer picks a word and the player
# has to guess it before he has made 7 wrong
# guesses of letters in the word.
# Each time the user makes an incorrect guess
# of a letter, one more body part gets added
# to the text graphic.
# Uses multiple defined functions.
# Uses branching and looping.
# (today's date goes here)
# (programmer names go here)
# (tester names go here)
# Possible future enhancements:
# Unresolved bugs:
############################### This section contains important information about the program.
The comments give an overview of how the hangman game will be played.

FUNCTION DEFINITION SECTION
# Definitions: print_game_rules(),
# display_figure(), prompt_for_letter()

In this program you’ll define three functions: print_game_rules(), which prints the game rules for the user; display_figure(), which displays the appropriate version of the guillotine and “hanging” stick figure; and prompt_for_letter(), which prompts the user for the next letter guess.
These functions are all called from the main processing section of the program

TrustyTony commented: Very unclear post +0
from turtle import *
from random import *
import winsound
def shooter_clicked(x,y):
    global endloop
    
    endloop =True
    return
setup(600,400)


maxx = 300

maxy = 200

minx= -maxx

miny= - maxy

bounce_point = 20
x = randint(minx + bounce_point, maxx + bounce_point)
y = randint(miny + bounce_point, maxy + bounce_point)

dx = 5
dy = 5
ball.goto(x,y)

ball.showturtle()

onscreenclick(stop_loop)



loop_count=0
endloop = False


while not endloop:


    xx = x + dx


    if xx < minx-bounce_point:
        xx = minx + bounce_point
        dx = -dx
        winsound.MessageBeep(-1)

    if xx > maxx - bounce_point:
        xx = maxx- bounce_point
        dx = -dx
        winsound.MessageBeep(-1)



    yy=y+dy
    if yy < miny - bounce_point: 
        yy= miny + bounce_point
        dy = -dy
        winsound.MessageBeep(-1)

    if yy > maxy- bounce_point:   
        yy = maxy - bounce_point
        dy = -dy
        winsound.MessageBeep(-1)


    x = xx
    y = yy
    ball.goto(x,y)
 

bye()

Dani AI

Generated

Note: posted a mistaken turtle/winsound snippet first and then the hangman implementation in Post #3. The code is a solid start for a classroom hangman, but it has a few logical bugs, Python-version issues and portability rough edges that will bite you when you run it. Below are focused, actionable fixes and small improvements to make the game robust and easier to maintain.

  • Input & Python version: the input routine should both strip and lowercase the answer and, for Python 3, use input() instead of raw_input(). Assign the cleaned value back (for example, letter = letter.strip().lower()), and validate with if len(letter) == 1 and letter.isalpha():.
  • Win condition and repeated letters: don’t increment a single letters_correct by 1 regardless of how many occurrences a letter has. Instead compute how many new positions you reveal and add that count, or simply test the win with if '_' not in guesses: after updating guesses.
  • Avoid fragile alphabet checks: the manual alphabet string in the post contains omissions/typos. Use str.isalpha() for validation and track tried letters with a set() (faster membership testing) rather than a concatenated string.
  • display_figure / max_incorrect: ensure your ASCII-art list length matches max_incorrect + 1, or clamp the index before printing to avoid out-of-range errors.
  • Portability & UX: winsound is Windows-only — guard it with a platform check or wrap in try/except. Show remaining attempts, the masked word with spaces (_ _ a _), and a list of wrong guesses to help the player.
  • Code structure: move logic into small functions (get_letter, reveal_letters, display_status, main) and add if __name__ == "__main__": main() so the module is import-safe and easier to test.

Quick test cases to run after fixes: words with repeated letters (e.g., "banana"), uppercase input, empty input, and non-alpha characters. These checks will catch the common edge cases in the current implementation.

jib -2 Junior Poster

Sorry fellas!! must be overexcitement. That is the wrong code

jib -2 Junior Poster
from random import *

def print_game_rules(max_incorrect,word_len):
    word_len=1
    print"you have only 7 chances to guess the right answer"
    return

def get_letter():
    print
    letter=raw_input("Guess a letter in the mystery word:") 
    letter.strip()
    letter.lower()
    print
    return letter
def display_figure(bad_guesses):
    graphics = [
    """
        +--------+
         |
         |
         |
         |
         |
         |
     ====================
    """,
    """
        +-------
        |      o
        |
        |
        |
        |
    ====================
    """,
    """
        +-------
        |      o
        |   ---+---
        |
        |
        |
    ======================
    """,
    """
        +-------
        |       o
        |    ---+---
        |       |
        |    
        |  
        |   
    =====================
    """,
    """
        +-------
        |  
        |       o
        |    ---+
        |       |
        |      / 
        |     /   
        |
        |
        |
        |
    =====================
    """,
    """
        +-------
        |        o
        |     ---+---
                 |
        |       / 
        |      /   
        |     /     
        |     
    =====================
    """,
      """
        +-------
        |        o
        |     ---+---
                 |
        |       / \
        |      /   \  
        |     /     \    
        |     
    =====================
    """
,]
   
    print graphics[bad_guesses]
    return



animals=["horse","donkey","dinosaur","monkey","cat","aligator","butterfly","buffallo","dragon"]
word=choice(animals)
word_len=len(word)
guesses=word_len * ['_']
max_incorrect=6
alphabet="abcdefghijklmnopqrstuvxyz"
letters_tried=""
number_guesses=0
letters_correct=0
incorrect_guesses=0


print_game_rules(max_incorrect,word_len)
while (incorrect_guesses != max_incorrect) and (letters_correct != word_len):
    letter=get_letter()
    if len(letter)==1 and letter.isalpha():
        if letters_tried.find(letter) != -1:
            print "You already picked", letter
        else:
            letters_tried = letters_tried + letter
            first_index=word.find(letter)
            if  first_index == -1:
                incorrect_guesses= incorrect_guesses +1
                print "the",letter,"is not the mystery_word"
            else:
                print"the",letter,"is in the mystery_word"
                letters_correct=letters_correct+1
                for i in range(word_len):
                    if letter == word[i]:
                        guesses[i] = letter

    else:
        print "Please guess a single letter in the alphabet."
    display_figure(incorrect_guesses)
    
    print ''.join(guesses)
    print "Letters tried so far: ", letters_tried
    if incorrect_guesses == max_incorrect:
        print "Sorry, too many incorrect guesses. You are hanged."
        print "the word was",word
    if letters_correct == word_len:
        print "You guessed all the letters in the word!"
        print "the word was",word
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.