您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
此存儲庫已封存,您能瀏覽檔案及複製此存儲庫,但不能推送、建立問題及拉取請求。
 
 
 
 
 
 

53 行
917 B

  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()