25개 이상의 토픽을 선택하실 수 없습니다. 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.

pendu.py 2.0 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import random
  2. import json
  3. def lire_scores():
  4. with open("scores.json") as f:
  5. scores = json.load(f)
  6. return scores
  7. def enregistrer_scores(scores):
  8. with open("scores.json", "w") as f:
  9. scores = json.dump(scores, f, indent=2)
  10. return scores
  11. def choisir_mot_au_hasard():
  12. fichier = open("mots.txt")
  13. contenu = fichier.read()
  14. fichier.close()
  15. mots = contenu.splitlines()
  16. n = len(mots)
  17. index = random.randint(0, n - 1)
  18. return mots[index]
  19. def demander_joueur():
  20. joueur = input("donnez votre nom: ")
  21. return joueur
  22. def jeu():
  23. mot = choisir_mot_au_hasard()
  24. print(mot)
  25. tentatives = []
  26. while True:
  27. afficher_indice(mot, tentatives)
  28. lettre = demander_lettre()
  29. tentatives += [lettre]
  30. score = len(tentatives)
  31. if a_gagné(mot, tentatives):
  32. print("Gagné")
  33. print(mot)
  34. return score
  35. def a_gagné(mot, tentatives):
  36. for c in mot:
  37. if c not in tentatives:
  38. return False
  39. return True
  40. def demander_lettre():
  41. print("entrer une lettre")
  42. lettre = input()
  43. return lettre
  44. def afficher_indice(mot, tentatives):
  45. for c in mot:
  46. if c in tentatives:
  47. print(c, end="")
  48. else:
  49. print("_", end="")
  50. print()
  51. def ajouter_resultat(scores, joueur_courant, score_courant):
  52. record = None
  53. for nom in scores:
  54. score = scores[nom]
  55. if record is None:
  56. record = score
  57. elif score < record:
  58. record = score
  59. if record is None:
  60. record = score_courant
  61. print(joueur_courant + ", vous avez établi le record à ", record)
  62. elif score_courant < record:
  63. print(joueur_courant + ", vous avez battu le record de ", record)
  64. scores[joueur_courant] = record
  65. return scores
  66. # main()
  67. anciens_scores = {"Bob": 30}
  68. joueur_courant = "Alice"
  69. score_courant = 28
  70. nouveaux_scores = ajouter_resultat(anciens_scores, joueur_courant, score_courant)
  71. print(nouveaux_scores)