Jan-20-2022, 05:32 PM
(This post was last modified: Jan-20-2022, 09:42 PM by Yoriz.
Edit Reason: Added code tags
)
Can someone tell me please how the code parts commented with
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 :)
# ********************
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)
Attached Files