Python Forum
Homework Hangman Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework Hangman Game
#1
Can someone tell me please how the code parts commented with # ******************** work?
It works but I don´t understand what they are doing.
My second problem ist the turtle. I want to draw the hangman but it makes a weird line between the code part when it switches between if and else when the user runs out of tries. Can you tell how I solve the problem?
Thx :)
#import the necessary modules
import random
import turtle

#import the lists
from categories import categorie_animal
from categories import categorie_flower
from categories import categorie_vehicle
from categories import categorie_clothes
from categories import categorie_colour
from categories import categorie_emotions

#Creat a turtle and hide the symbol
bob = turtle.Turtle()
bob.hideturtle()



def getLength(): # ********************
    wordLength = [] # ********************
    for i in range(len(randomWord)): # ********************
        wordLength.append('_ ') # ********************
    return wordLength # ********************

#ask the user which categorie he/she wants to play
randomWord = input("What categorie do you want to play? animal, flower, vehicle, clothes, colour or emotions? \n").lower()

#Check if the input is correct
while randomWord not in ["animal","flower","vehicle","clothes","colour","emotions"]:
        print ("Invalid input, please try again: \n")
        randomWord = input("What categorie do you want to play? animal, flower, vehicle, clothes, colour or emotions? \n ").lower()

else:
        print ("The categorie "+randomWord+" is a great choice!")

#Get a random word out of the chosen categorie
if randomWord == "animal":
    randomWord = random.choice(categorie_animal)
    wordLength = getLength()
    print("" . join(wordLength))

elif randomWord == "flower":
    randomWord = random.choice(categorie_flower)
    wordLength = getLength()
    print("" . join(wordLength))

elif randomWord == "vehicle":
    randomWord = random.choice(categorie_vehicle)
    wordLength = getLength()
    print("" . join(wordLength))

elif randomWord == "clothes":
    randomWord = random.choice(categorie_clothes)
    wordLength = getLength()
    print("" . join(wordLength))

elif randomWord == "colour":
    randomWord = random.choice(categorie_colour)
    wordLength = getLength()
    print("" . join(wordLength))

else:
    randomWord = random.choice(categorie_emotions)
    wordLength = getLength()
    print("" . join(wordLength))

tries = 8


gussedWord = list(getLength())

#Game
while tries > 0:
    if "".join(gussedWord) == randomWord:
        print("You are awesome! You gussed the correct word!") 
        break
    

    print("You get "+ str(tries) + " tries ")             
    gussedLetter = input("Guess a letter: \n").lower()    



   if gussedLetter in randomWord: # ********************
        print("Correct!") # ********************
        for i in range(len(randomWord)): # ********************
            if list(randomWord)[i] == gussedLetter: # ********************
                gussedWord[i] = gussedLetter # ********************
        print("".join(gussedWord)) # ********************

    else: 
        print("That´s a wrong letter!")             
        tries -= 1                                      

#Draw the hangman lines
    if tries == 7:
        bob.forward(100)

    elif tries == 6:
        bob.backward(50)
        bob.left(90)
        bob.forward(200)

    elif tries == 5:
        bob.right(90)
        bob.forward(100)

    elif tries == 4:
        bob.right(90)
        bob.forward (40)

    elif tries == 3:
        bob.right(90)
        bob.circle(15)

    elif tries == 2:
        bob.pencolor("white")
        bob.left(90)
        bob.forward(30)
        bob.pencolor("black")
        bob.forward(40)

    else:
        bob.backward(20)
        bob.right(135)
        bob.forward(20)
        bob.backward(20)
        bob.right(90)
        bob.forward(20)




#Game over    
else:
    bob.pencolor("red")
    bob.left(90)
    bob.forward(30)
    bob.right(45)
    bob.forward(20)
    bob.backward(20)
    bob.right(-90)
    bob.forward(20)

    print("You ran out of tries! The searched word is: "+randomWord)
Yoriz write Jan-20-2022, 09:42 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   Hangman_2.py (Size: 4.2 KB / Downloads: 161)
Reply
#2
The getLength() function returns a list of "_" based on the length of randomWord.

This code below informs you about the most recently guessed letter. If the letter is in the randomWord it prints correct. It also updates the gussedWord list, replacing "_" with the gussedLetter wherever randomWord[i] == gussedLetter
if gussedLetter in randomWord:
    print("Correct!")
    for i in range(len(randomWord)):
        if list(randomWord)[i] == gussedLetter:
           gussedWord[i] = gussedLetter
    print("".join(gussedWord))
Be forewarned that this is not good code. It is not bad enough to serve as an example of how not to write a hangman game, but it is not good.
Reply
#3
I think that you have forgotten the rules of Hangman. You only draw parts of Bob if the guess is incorrect. Anyway, Here is the fix to draw Bob correctly. I have eliminated the parts of the code that import things that are unavailable to me.
import turtle

#Creat a turtle and hide the symbol
bob = turtle.Turtle()
# bob.hideturtle()

def getLength():
	wordLength = []
	for i in range(len(randomWord)):
		wordLength.append('_ ')
	return wordLength

#ask the user which categorie he/she wants to play
randomWord = 'testing'
wordLength = getLength()
print("" . join(wordLength))

tries = 8
gussedWord = list(getLength())

#Game
while tries > 0:
	if "".join(gussedWord) == randomWord:
		print("You are awesome! You gussed the correct word!")
		break

	print("You get "+ str(tries) + " tries ")
	gussedLetter = input("Guess a letter: \n").lower()

	if gussedLetter in randomWord:
		print("Correct!")
		for i in range(len(randomWord)):
			if list(randomWord)[i] == gussedLetter:
				gussedWord[i] = gussedLetter
				print("".join(gussedWord))
	else:
		print("That´s a wrong letter!")
	tries -= 1
	#Draw the hangman lines
	if tries == 7:
		bob.forward(100)
	elif tries == 6:
		bob.backward(50)
		bob.left(90)
		bob.forward(200)
	elif tries == 5:
		bob.right(90)
		bob.forward(100)
	elif tries == 4:
		bob.right(90)
		bob.forward (40)
	elif tries == 3:
		bob.right(90)
		bob.circle(15)
	elif tries == 2:
		bob.pencolor("white")
		bob.left(90)
		bob.forward(30)
		bob.pencolor("black")
		bob.forward(40)
	elif tries == 1:
		bob.backward(20)
		bob.right(135)
		bob.forward(20)
		bob.backward(20)
		bob.right(90)
		bob.forward(20)
	#Game over
	else:
		bob.backward(20)
		bob.right(135)
		bob.forward(30)
		bob.right(45)
		bob.forward(20)
		bob.backward(20)
		bob.right(-90)
		bob.forward(20)

print("You ran out of tries! The searched word is: "+randomWord)
Reply
#4
There should be a function do draw the hangman. There should be a function to get the random word. There should be a function to enter the guess.

This is my draw hangman function.
import turtle
def drawHangman(segment):
    """Draw the hangman segments"""
    if segment == 1:  # Gallows
        bob.forward(100)
        bob.backward(50)
        bob.left(90)
        bob.forward(200)
        bob.right(90)
        bob.forward(100)
        bob.right(90)
        bob.forward (40)
    elif segment == 2: # Head
        bob.right(90)
        bob.circle(20)
    elif segment == 3: # Body
        bob.up()
        bob.left(90)
        bob.forward(40)
        bob.down()
        bob.forward(60)
    elif segment == 4: # Arm
        bob.backward(40)
        bob.right(135)
        bob.forward(30)
        bob.backward(30)
    elif segment == 5: # Arm
        bob.right(90)
        bob.forward(30)
        bob.backward(30)
    elif segment == 6: # Leg
        bob.right(135)
        bob.forward(40)
        bob.right(45)
        bob.forward(30)
        bob.backward(30)
    elif segment == 7: # Leg
        bob.left(90)
        bob.forward(30)
        bob.backward(30)
    elif segment == 8: # Extra surprise segment so we have 8
        bob.up()
        bob.right(45)
        bob.backward(100)
        bob.down()
        bob.pencolor("red")
        bob.pensize(3)
        for x in range(2, 8):
            drawHangman(x)

bob = turtle.Turtle()
bob.hideturtle()

for segment in range(1, 9):
    drawHangman(segment)
    input()
It differs a bit from the "official" hangman rules to allow for 8 misses (usually 6). Normally you start out with the gallows before the first guess and I had to add an extra step at the end.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework: Smiley Face game itmustbebunnies 3 6,587 Jun-08-2019, 08:07 AM
Last Post: Yoriz
  Hangman game jsirota 2 3,723 Nov-06-2017, 06:39 PM
Last Post: gruntfutuk
  Hangman-Game (German code) .. Unkreatief 1 3,725 Mar-22-2017, 10:30 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

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