pendu.py

Created by schraf

Created on October 01, 2019

1.31 KB

Explications en vidéo

Voici le célèbre jeu du pendu. Pour cela vous aurez besoin d’une autre personne qui tapera le mot ou la phrase secrète puis ce sera à vous de jouer.

Modifications possibles :

  • Ajouter une liste de mots prédéfinis pour ne pas avoir besoin d’un autre joueur :
from random import *
mot=["CHIEN","CHAT","CRAYON","FEUILLE"]
secret = choice(mot)
  • Modifier le visuel du pendu pour avoir plus de coups, par exemple en 10 coups :
  |
  o
/-+-/
  |
  |
 / \


def eff():
  for i in range(15):print()

def question(txt):
  rep = ""
  while rep == "":
    rep = input(txt+" ? ")
    rep = rep.upper()
    if any([x<"A" or x>"Z" for x in rep]):
      print("Que des lettres !")
      rep = ""
  return rep

def dessin(c):
  print("+--+")
  print("|/ |")
  for i in range(4):
    print("| ",end="")
    if i == 0 and c > 0: print(" o")
    elif i == 1 and c >= 2:
      if c <= 3: print([" |","/|"][c-2])
      else: print("/|\\")
    elif i == 2 and c >= 5:
      if c == 5: print("/")
      else: print("/ \\")
    else: print("")  
  print("="*5)
  return

def go():
  secret = question("Mot secret")
  coup = 0
  tests = []
  trouve = ["_"]*len(secret)
  eff() 
  bon = 0
  while True:  
    lettre = question("Lettre")[0]
    ajout = True
    for (i,c) in enumerate(secret):
      if lettre == c:
        if trouve[i] != c: bon += 1
        trouve[i] = c
        ajout = False
    if bon == len(secret):
      print("BRAVO pour",secret)
      print("En",coup,"coup(s) !")
      return
    eff()
    if ajout: coup += 1
    dessin(coup)
    if ajout:
      tests.append(lettre)
      tests = sorted(tests)
      if coup == 6:
        print("PERDU !")
        print("Secret =",secret)
        break
    print("Vos tests :","".join(tests))
    print("Secret :","".join(trouve))
 
go()