Hangman Projekt
Verfasst: Donnerstag 20. Januar 2022, 18:51
Hallo, ich habe eine Frage zu diesem Programm. Teilweise habe ich es selbst geschrieben, aber zwei Parts habe ich von einer Quelle und verstehe sie nicht ganz. Vielleicht kann mir jemand erklären, was dabei genau passiert.
Außerdem habe ich eine komische Linie, wenn die Zeichnung von turtle zwischen den if, elif und else auf das letzte else springt.
Vielleicht kann mir jemand hier helfen.
Vielen dank und LG
#import the necessary modules
import random
import turtle
#import the lists out of a seperate file
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) == gussedLetter:
gussedWord = gussedLetter
print("".join(gussedWord))
else:
print("That´s a wrong letter!") #Print it ´s wrong
tries -= 1 #Reduce the tries
#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.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)
Außerdem habe ich eine komische Linie, wenn die Zeichnung von turtle zwischen den if, elif und else auf das letzte else springt.
Vielleicht kann mir jemand hier helfen.
Vielen dank und LG
#import the necessary modules
import random
import turtle
#import the lists out of a seperate file
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) == gussedLetter:
gussedWord = gussedLetter
print("".join(gussedWord))
else:
print("That´s a wrong letter!") #Print it ´s wrong
tries -= 1 #Reduce the tries
#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.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)