This code ran 4-5 times without a problem, but it does not anymore. Whenever I run the file, the window for Python Turtle Graphics stops responding,(freezes) and eventually crashes.
import random
import turtle
import package
running = True
words = ['aardvark', 'camel', 'baboon']
found_letters = []
hangman = package.Man(turtle.Turtle())
word = str(random.choice(words))
listed_word = []
for character in word:
listed_word.append(character)
blanks = ''
lives = 6
for character in word:
blanks += '_'
print(blanks)
while running:
guess = str(input(f"Enter your letter: ")).lower()
if len(guess) == 1:
if guess in listed_word:
for index, letter in enumerate(listed_word):
if letter == guess:
blanks_list = list(blanks)
blanks_list[index] = guess
blanks = ''.join(blanks_list)
print(blanks)
if blanks == word:
print("You win!")
turtle.done()
running = False
else:
if lives != 0:
lives -= 1
hangman.check(lives)
if lives > 0:
print(f"You have {lives} lives left.")
else:
print("Game over!")
turtle.done()
running = False
else:
pass
I'm not sure if the issue is calling the check function inside of a loop, or it initializing the class.
Here's the 'package' file with the 'Man' class.
import turtle
class Man:
def __init__(self, turtle_class: turtle.Turtle):
self.pen = turtle_class
self.pen.penup()
self.pen.goto(0, 100)
def circle(self):
self.pen.pendown()
self.pen.circle(30)
self.pen.penup()
def body(self):
self.pen.pendown()
self.pen.right(90)
self.pen.forward(60)
self.pen.penup()
def left_leg(self):
self.pen.pendown()
self.pen.left(30)
self.pen.forward(60)
self.pen.penup()
self.pen.back(60)
self.pen.right(30)
def right_leg(self):
self.pen.pendown()
self.pen.right(30)
self.pen.forward(60)
self.pen.penup()
self.pen.back(60)
self.pen.left(30)
def left_arm(self):
self.pen.left(180)
self.pen.forward(46.6666666667)
self.pen.left(90)
self.pen.left(30)
self.pen.pendown()
self.pen.forward(46.6666666667)
self.pen.penup()
self.pen.left(180)
self.pen.forward(46.6666666667)
self.pen.left(30)
def right_arm(self):
self.pen.right(90)
self.pen.pendown()
self.pen.forward(46.6666666667)
self.pen.penup()
def check(self, value):
if value == 5:
self.circle()
elif value == 4:
self.body()
elif value == 3:
self.left_leg()
elif value == 2:
self.right_leg()
elif value == 1:
self.left_arm()
The check function is the only function that is called in the main file.
I did notice whenever I tried it in the 'package' file I made with the class, it automatically closes after initialization because I'm not doing anything with it.
However, in the main file whenever I initialize my class (the variable at the top named hangman) it stays even if I don't put it in a loop or do anything with it. (during testing)
When I remove everything associated with the class and the 'package' file the game works just fine.
The import package
works fine. In fact in my testing i simply replaced this with the actual class (just in case).
The error turns out to be in the loop.
I fixed this by using turtle.bye()
instead of turtle.done()
.
This succesfully terminates the loop.
this is the (new) code for the loop:
while running:
guess = str(input(f"Enter your letter: ")).lower()
if len(guess) == 1:
if guess in listed_word:
for index, letter in enumerate(listed_word):
if letter == guess:
blanks_list = list(blanks)
blanks_list[index] = guess
blanks = ''.join(blanks_list)
print(blanks)
if blanks == word:
print("You win!")
turtle.bye()
running = False
else:
if lives != 0:
lives -= 1
hangman.check(lives)
if lives > 0:
print(f"You have {lives} lives left.")
else:
print("Game over!")
turtle.bye()
running = False
else:
pass