top of page

Making a Hang-man game

  • Writer: 라임 샹큼
    라임 샹큼
  • Feb 24
  • 2 min read

While revising loops such as while and for, I decided to make a hang-man game: the childhood game that everybody used to play during recess.


The importance of planning

Compared to other games I tried to make, the hangman game required a bit of plotting. This proved hard to do with only visualizing it in my head because it had quite a few aspects I needed to include. This made me resort to a flowchart.

display = ""

I don't know how to use it properly but it helped me visualize my project better.


Encountering errors(with little to no context)

display = ""while display != chosen_word:    guess = input("Guess a letter: ").lower()    for letter in chosen_word:        if letter == guess and letter not in display:            display += letter        else:            display += "_"    print(display)

In my first attempt, I tried to make the while loop stop when the word matched up with the displayed word.

I'd removed the display variable from the loop and positioned it so that it would not be refreshed with every loop but this made it so that the display variable continued to have elements added to it, making it longer and longer as the loop went on.

This was not ideal.

Instead of making the loop run until the display was equal to the chosen_word, I rewrote the code so that the loop would run until it ran into a bit of code that told it it was game over.

I would later need to add this tidbit to inform it to end the loop.


game_over = False

correct_letters = []


while not game_over:

    guess = input("Guess a letter: ").lower()


    display = ""


    for letter in chosen_word:

        if letter == guess:

            display += letter

            correct_letters.append(guess)

        elif letter in correct_letters:

            display += letter

        else:

            display += "_"


    print(display)


    if "_" not in display:

        game_over = True

        print("You win.")


By fixing it like so, I was able to achieve the ideal result.


Other things I learned

  • Append is to only be used with lists and although strings are iterable, to add something to a string, you would need to add it in

  • When making the new variable for for loops, they don't have to different at all times:

    for n in letters...

    for n in numbers....

    .

    .

  • For indentation, 4 spaces is needed but for some coding platforms, it makes it so that you can control how many spaces a tab key can jump


file:///Users/gaeunlee/PycharmProjects/main.py.html

Recent Posts

See All
Reading and writing files

I learned how to read write txt files. I learned how to navigate files. I always wondered how people sent automated emails to so many...

 
 
 
Ping-Pong game

This project really helped me understand OOP more and grasp the concept of inheritance in classes. screen = Screen() screen.setup(height...

 
 
 
Making the well-known Snake game

Everyone has atleast had one instance in which they downloaded snake.io on their phone and had a few goes at it. I wanted to replicate...

 
 
 

Comments


© 2024 by GifTED. Powered and secured by Wix

bottom of page