From 1b152f44820d8b05362be5addeda7fd720a23ad4 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 14 Sep 2020 15:58:36 +0200 Subject: [PATCH] Add task --- 10.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 10.py diff --git a/10.py b/10.py new file mode 100644 index 0000000..ac7dec5 --- /dev/null +++ b/10.py @@ -0,0 +1,53 @@ +import os + +class game(): + def __init__(self): + self.secret_word = input('Skriv inn det hemmelige ordet: ') + self.lives = int(input('Hvor mange forsøk får brukeren? ')) + self.lettersLeft = list(self.secret_word) + os.system('clear') + + def getWord(self): + return ''.join([('*' if (ch in self.lettersLeft) else ch) for ch in list(self.secret_word)]) + + def removeLetterFromLettersLeft(self, letter): + self.lettersLeft = list(filter(lambda leftLetter: leftLetter != letter, self.lettersLeft)) + + def gameOver(self): + print('Du har ingen liv igjen.') + exit(0) + + def gameWon(self): + print(f'Gratulerer. Ordet var {self.secret_word}') + exit(0) + + def wrongLetter(self, letter): + print(f'Bokstaven {letter} er ikke i ordet.') + self.lives -= 1 + if self.lives == 0: + self.gameOver() + print(f'Du har {self.lives} liv igjen, prøv på nytt.') + + def rightLetter(self, letter): + print('Stemmer, bokstaven er i ordet') + self.removeLetterFromLettersLeft(letter) + if self.lettersLeft == []: + self.gameWon() + + def update(self): + print(self.getWord()) + letter = input('Gjett på én bokstav i ordet: ') + if letter in self.lettersLeft: + self.rightLetter(letter) + else: + self.wrongLetter(letter) + + def loop(self): + while True: + os.system('clear') + self.update() + input("Trykk enter for å fortsette...") + +if __name__ == "__main__": + myGame = game() + myGame.loop() \ No newline at end of file