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.
Deze repo is gearchiveerd. U kunt bestanden bekijken en het klonen, maar niet pushen of problemen/pull-requests openen.

04-pendu.py 929 B

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