I am a total beginner and I tried to make a hangman game, can someone tell me if there are bad spots?
This is my second ever project. The first one, (a number guessing game) I just gave up on it because everything got really messy because I used no functions at all. However now I'm wondering if I used way too many functions because most are only called once. The game works perfectly but I'd like to know if it could be improved. I tried using LLMs to review but I don't trust ChatGPT glazing me.
This line <locations = [pos for pos, char in enumerate(word) if char == guess]> is the only one lifted directly from stackexchange without my understanding, because I couldn't figure out my own solution for it, so I don't get how that works lol. I hope someone can explain it. and ChatGPT said I should use less global variables and more parameters but I'm not very comfortable with those yet so I decided not to.
Basically my questions are:
Am I using too many functions
Is my code inefficient
How does <locations = [pos for pos, char in enumerate(word) if char == guess]> work
Btw I am worried the code is very long for people to spend their time on, but half of it is just the turtle drawing which can be skipped ig
from random_word import RandomWords
from turtle import *
r = RandomWords()
def gallows():
up()
left(180)
forward(100)
right(90)
down()
backward(200)
left(90)
forward(100)
up()
backward(100)
down()
backward(100)
up()
forward(100)
right(90)
forward(200)
down()
forward(200)
right(90)
forward(100)
right(90)
forward(50)
def head():
right(90)
circle(35)
up()
circle(35, 180)
right(90)
down()
def body():
forward(150)
up()
backward(150)
down()
def leftarm():
left(45)
forward(100)
up()
backward(100)
right(45)
down()
def rightarm():
right(45)
forward(100)
up()
backward(100)
left(45)
def leftleg():
forward(150)
left(45)
down()
forward(100)
up()
backward(100)
right(45)
down()
def rightleg():
right(45)
forward(100)
up()
backward(100)
left(45)
down()
hangman = [head, body, leftarm, rightarm, leftleg, rightleg]
def startGame():
global i
global wguessList
global rguessList
while True:
start = input('Start game/Exit - S/E: ').upper()
if start == 'S':
i = -1
clearscreen()
wguessList = []
rguessList = []
gallows()
return
elif start == 'E':
quit()
else:
print('Enter S or E only')
continue
def makeBlanks():
global word
global board
word = r.get_random_word()
board = ['_'] * len(word)
return word, board
def takeGuess():
global guess
while True:
guess = input('Enter your letter guess: ').lower()
if len(guess) != 1:
print('Enter one letter only')
continue
elif guess.isalpha() == False:
print('Enter English letters only')
else:
return guess
def wrongAnswer():
global i
global wguessList
global guess
global board
if guess in wguessList:
print('You already guessed this incorrectly')
return
if i < len(hangman) - 1:
i += 1
print('Wrong')
wguessList.append(guess)
print(''.join(board))
print('Past wrong guesses: ' + ', '.join(wguessList))
hangman[i]()
def rightAnswer():
global wguessList
global rguessList
global board
if guess in rguessList:
print('You already guessed this correctly')
return
locations = [pos for pos, char in enumerate(word) if char == guess]
for x in locations:
board[x] = guess
print('Right')
print(''.join(board))
print('Past wrong guesses: ' + ', '.join(wguessList))
rguessList.append(guess)
print('-------------------- Hangman --------------------')
startGame()
while True:
word, board = makeBlanks()
print(''.join(board))
while True:
guess = takeGuess()
if guess in word:
rightAnswer()
if '_' not in ''.join(board):
print('You got it!')
startGame()
break
elif guess not in word:
wrongAnswer()
if i == len(hangman) - 1:
print('Sorry, you\'re out of chances!')
print('The word was ' + word)
startGame()
break
done()