You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

04-pendu.py 917 B

6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import random
  2. def read_words():
  3. file = open("pendu.txt")
  4. contents = file.read()
  5. file.close()
  6. words = contents.splitlines()
  7. return words
  8. def choose_word(words):
  9. n = len(words)
  10. index = random.randint(0, n-1)
  11. return words[index]
  12. def has_won(word, letters):
  13. for letter in word:
  14. if letter not in letters:
  15. return False
  16. return True
  17. def display_hint(word, letters):
  18. for letter in word:
  19. if letter in letters:
  20. print(letter, end="")
  21. else:
  22. print("_", end="")
  23. print("")
  24. def main():
  25. words = read_words()
  26. word = choose_word(words)
  27. print(word)
  28. letters = set()
  29. display_hint(word, letters)
  30. while True:
  31. new_letter = input()
  32. letters.add(new_letter)
  33. display_hint(word, letters)
  34. if has_won(word, letters):
  35. print("Gagné")
  36. return
  37. main()