Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman Game
#1
I've been teaching myself how to code for a few weeks and did a hangman game all by myself! I'm so chuffed ! Such a good feeling and I still can't believe it works!
I would love some tips about things to look out for, maybe something to clean the code a little? Be gentle as I'm new and super excited this all worked out haha

import random

def making_a_guess():
    x = 0
    global update_display
    correct_guess = False
    for letter in chosen_word:
        if guess.lower() == chosen_word[x]:
            blank_list[x] = guess.lower()
            correct_guess = True
        x += 1
    if correct_guess == False:
        print(f"There is no {guess}, sorry.")
        update_display += 1
    x = 0


HANGMANPICS = ['''
  +---+
  |   |
      |
      |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========''', '''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========''']

word_list = ["aardvark", "baboon", "camel", "jazz", "grass", "follow", "castle", "cloud"]

chosen_word = list(random.choice(word_list))

blank = ""
for letter in chosen_word:
    blank += "_"
blank_list = list(blank)

update_display = 0

#----------------------------------------------------------------------------------------------

print(HANGMANPICS[update_display])
guess = input(f"Welcome to hangman.\n{blank}\nMake a guess? ")
making_a_guess()
print(HANGMANPICS[update_display])
print(''.join(blank_list))
while update_display < 6:
    if blank_list == chosen_word:
        print("YOU WIN!")
        break
    guess = input("Make another guess? ")
    making_a_guess()
    print(HANGMANPICS[update_display])
    print(''.join(blank_list))
if update_display == 6:
    print("GAME OVER.")
Reply
#2
if correct_guess == False:
Better:
if not correct_guess:
referring to line 69 and some following lines:
word_list = ["aardvark", "baboon", "camel", "jazz", "grass", "follow", "castle", "cloud"]
I would always write global variables in uppercase like WORD_LIST instead of word_list

i would use more comments. While writing simple scripts its not really necessary, it can help a lot in the future if you just used to comment your code. I am a huge fan of purpose statements

blank = ""
for letter in chosen_word:
    blank += "_"
blank_list = list(blank)
you can just write:
blank = "_" * len(chosen_word)
If you are only coding for weeks this is pretty good btw
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman game- feedback please NV12 1 2,078 Apr-08-2020, 09:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020