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: 169)
Reply


Messages In This Thread
Homework Hangman Game - by jagr29 - Jan-20-2022, 05:32 PM
RE: Homework Hangman Game - by deanhystad - Jan-20-2022, 09:17 PM
RE: Homework Hangman Game - by BashBedlam - Jan-20-2022, 09:37 PM
RE: Homework Hangman Game - by deanhystad - Jan-20-2022, 10:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework: Smiley Face game itmustbebunnies 3 6,666 Jun-08-2019, 08:07 AM
Last Post: Yoriz
  Hangman game jsirota 2 3,779 Nov-06-2017, 06:39 PM
Last Post: gruntfutuk
  Hangman-Game (German code) .. Unkreatief 1 3,776 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