cours_progra/bac1/q1/13oct/hangman.py
Debucquoy 4fd7542f03
.
2023-09-20 15:18:20 +02:00

59 lines
1.6 KiB
Python

from hangmantui import hangman, clear
from userInput import ask_word_in_dictionary, ask_letter
def hangman_start(score: int):
"""start the hangman """
life = 10
choosed_word = ask_word_in_dictionary()
found_letters = '*' * len(choosed_word)
not_in_word = ''
while life > 0:
draw(life, found_letters)
in_letter = ask_letter(not_in_word)
if in_letter in choosed_word:
found_letters = unhide_letters(choosed_word, in_letter, found_letters)
else:
not_in_word += in_letter
life -= 1
if found_letters == choosed_word:
clear()
print('CONGRATS!! the word was: ', choosed_word)
score += len(list(dict.fromkeys(choosed_word[:]))) + life - len(not_in_word)
break
return score
def unhide_letters(choosed_word, letter, found_letters):
"""take a letter and unhide it in the found letters
:choosed_word: the original word
:letter: the letter to unhide
:found_letters: the current found letter
:returns: found letter with unhiden letters
"""
ret = ''
for i in range(len(choosed_word)):
if letter in choosed_word[i]:
ret += letter
else:
ret += found_letters[i]
return ret
def draw(life, word):
"""draw the main screen with hangman, life remaining and word with * on unknown letters
:life: int of life remaining
:word: current word with * on unknown letters and other letters on found
"""
clear()
print(life)
hangman(life)
print(word)
if __name__ == "__main__":
print("your score is", hangman_start(0))