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.
 
 
 
 
 
 

80 regels
1.6 KiB

  1. import sys
  2. import random
  3. def read_words():
  4. file = open("words.txt")
  5. contents = file.read()
  6. file.close()
  7. words = contents.splitlines()
  8. return words
  9. def choose_word(words):
  10. n = len(words)
  11. index = random.randint(0, n - 1)
  12. return words[index]
  13. def has_won(word, letters):
  14. for letter in word:
  15. if letter not in letters:
  16. return False
  17. return True
  18. def display_hint(word, letters):
  19. for letter in word:
  20. if letter in letters:
  21. print(letter, end="")
  22. else:
  23. print("_", end="")
  24. print("")
  25. def main():
  26. num_tries = 10
  27. words = read_words()
  28. word = choose_word(words)
  29. letters = set()
  30. display_hint(word, letters)
  31. while num_tries:
  32. print(num_tries, "essai(s) restant(s)")
  33. answer = input()
  34. while answer in letters:
  35. print("Lettre déjà proposée")
  36. answer = input()
  37. if len(answer) == len(word):
  38. guess = answer
  39. if guess == word:
  40. print("Gagné")
  41. sys.exit(0)
  42. else:
  43. print("Mauvaise réponse")
  44. num_tries -= 1
  45. elif len(answer) == 1:
  46. letter = answer
  47. letters.add(letter)
  48. display_hint(word, letters)
  49. if has_won(word, letters):
  50. print("Gagné")
  51. sys.exit(0)
  52. else:
  53. num_tries -= 1
  54. else:
  55. print("Veuillez entrer juste une lettre ou le mot entier")
  56. print("Vous avez dépassé le nombre d'essais autorisés")
  57. print("Le mot à deviner était :", word)
  58. sys.exit(1)
  59. main()