J'ai besoin d'aide pour le jeu du pendu sur Python

Fermé
Marie27300 Messages postés 1 Date d'inscription samedi 23 mai 2015 Statut Membre Dernière intervention 23 mai 2015 - Modifié par NHenry le 23/05/2015 à 21:06
 Utilisateur anonyme - 24 mai 2015 à 15:34
Bonsoir, j'ai un problème je ne comprend pas pourquoi mon programme ne marche pas .. Je vous le met ci-joint:
 # Le jeu du pendu par Thibault, Marie et Maxence
 
from tkinter import *
from random import choice
 
fichier1 = open("liste_a.txt", "r")
fichier2 = open("liste_s.txt", "r")
fichier3 = open("liste_f.txt", "r")
liste_a = fichier1.readlines()   # met tous les mots du fichier dans une liste
fichier1.close()
liste_s = fichier2.readlines()
fichier2.close()
liste_f = fichier3.readlines()
fichier3.close()

prenom = input("Entrez votre prénom : ")
print("Bonjour,", prenom)

fich=input("Avec quelle catégorie voulez-vous jouer ?(animaux(1)/sports(2)/fruits(3))") ### Demande à l'utilisateur le niveau de difficulté
if fich==1:
    
    def lettre_dans_mot(lettre):
        global partie_en_cours, mot_partiel, mot_choisi, nb_echecs, image_pendu
        global bouton, felicitation
        if partie_en_cours :
            nouveau_mot_partiel = ""
            lettre_dans_mot = False
            i=0
            while i<len(mot_choisi):
                if mot_choisi[i]==lettre:
                    nouveau_mot_partiel = nouveau_mot_partiel + lettre
                lettre_dans_mot = True 
            else:
                nouveau_mot_partiel = nouveau_mot_partiel + mot_partiel[i]
                i+=1
            mot_partiel = nouveau_mot_partiel
            afficher_mot(mot_partiel)
            bouton[ord(lettre)-65].config(text="  ")
            if not lettre_dans_mot :        # lettre fausse. Changer le dessin.
                nb_echecs += 1
                nomFichier = "pendu_"+str(nb_echecs)+".gif"
                photo=PhotoImage(file=nomFichier)
                image_pendu.config(image=photo)
                image_pendu.image=photo
                if nb_echecs == 7:  # trop d'erreurs. Fini.
                    partie_en_cours = False
                    afficher_mot(mot_choisi)
            elif mot_partiel == mot_choisi:  # le mot a était trouvée !
                partie_en_cours = False
                canevas.itemconfig(felicitation,text="bravo " + prenom +" !")
    def afficher_mot(mot):
        global lettres
        mot_large = ""
        i=0
        while i<len(mot):  # ajoute un espace entre les lettres
            mot_large = mot_large + mot[i] + " "
            i+=1
        canevas.delete(lettres)
        lettres = canevas.create_text(320,60,text=mot_large,fill='black',font='Courrier 30')
    def init_jeu():
        global mot_choisi, mot_partiel, image_pendu, lettres, nb_echecs
        global partie_en_cours, liste_a, bouton, felicitation
        for i in range(26):
            bouton[i].config(text=chr(i+65))
        nb_echecs = 0
        partie_en_cours = True
        mot_choisi = choice(liste_a).rstrip()
        mot_partiel = "-" * len(mot_choisi)
        afficher_mot(mot_partiel)
        photo=PhotoImage(file="pendu_0.gif")
        image_pendu.config(image=photo)
        image_pendu.image=photo
        canevas.itemconfig(felicitation,text="")
fenetre = Tk()
fenetre.title("Le jeu du pendu par Thibaut, Marie et Maxence.")
canevas = Canvas(fenetre, bg='white', height=500, width=620)
canevas.pack(side=BOTTOM)
bouton = [0]*26
for i in range(26):
    bouton[i] = Button(fenetre,text=chr(i+65),command=lambda x=i+65:lettre_dans_mot(chr(x)))
    bouton[i].pack(side=LEFT)
bouton2 = Button(fenetre,text='Quitter',command=fenetre.quit)
bouton2.pack(side=RIGHT)
bouton1 = Button(fenetre,text='Recommencer',command=init_jeu)
bouton1.pack(side=RIGHT)
photo=PhotoImage(file="pendu_0.gif")
image_pendu = Label(canevas, image=photo, border=0)
image_pendu.place(x=120, y=140)
lettres = canevas.create_text(320,60,text="",fill='black',font='Courrier 30') 
felicitation=canevas.create_text(320,110,text="",fill='green',font='Courrier 40')
init_jeu()
fenetre.mainloop()
fenetre.destroy()

    
if fich==2:
    def lettre_dans_mot(lettre):
        global partie_en_cours, mot_partiel, mot_choisi, nb_echecs, image_pendu
        global bouton, felicitation
        if partie_en_cours :
            nouveau_mot_partiel = ""
            lettre_dans_mot = False
            i=0
            while i<len(mot_choisi):
                if mot_choisi[i]==lettre:
                    nouveau_mot_partiel = nouveau_mot_partiel + lettre
                    lettre_dans_mot = True 
                else:
                    nouveau_mot_partiel = nouveau_mot_partiel + mot_partiel[i]
                i+=1
            mot_partiel = nouveau_mot_partiel  
            afficher_mot(mot_partiel)
            bouton[ord(lettre)-65].config(text="  ")
            if not lettre_dans_mot :        # lettre fausse. Changer le dessin.
                nb_echecs += 1
                nomFichier = "pendu_"+str(nb_echecs)+".gif"
                photo=PhotoImage(file=nomFichier)
                image_pendu.config(image=photo)
                image_pendu.image=photo
                if nb_echecs == 7:  # trop d'erreurs. Fini.
                    partie_en_cours = False
                    afficher_mot(mot_choisi)
            elif mot_partiel == mot_choisi:  # le mot a était trouvée !
                partie_en_cours = False
                canevas.itemconfig(felicitation,text="bravo " + prenom +" !")
    def afficher_mot(mot):
        global lettres
        mot_large = ""
        i=0
        while i<len(mot):  # ajoute un espace entre les lettres
            mot_large = mot_large + mot[i] + " "
            i+=1
        canevas.delete(lettres)
        lettres = canevas.create_text(320,60,text=mot_large,fill='black',font='Courrier 30')
    def init_jeu():
        global mot_choisi, mot_partiel, image_pendu, lettres, nb_echecs
        global partie_en_cours, liste_s, bouton, felicitation
        for i in range(26):
            bouton[i].config(text=chr(i+65))
        nb_echecs = 0
        partie_en_cours = True
        mot_choisi = choice(liste_s).rstrip()
        mot_partiel = "-" * len(mot_choisi)
        afficher_mot(mot_partiel)
        photo=PhotoImage(file="pendu_0.gif")
        image_pendu.config(image=photo)
        image_pendu.image=photo
        canevas.itemconfig(felicitation,text="")
    fenetre = Tk()
    fenetre.title("Le jeu du pendu par Thibaut, Marie et Maxence.")
    canevas = Canvas(fenetre, bg='white', height=500, width=620)
    canevas.pack(side=BOTTOM)
    bouton = [0]*26
    for i in range(26):
        bouton[i] = Button(fenetre,text=chr(i+65),command=lambda x=i+65:lettre_dans_mot(chr(x)))
        bouton[i].pack(side=LEFT)
    bouton2 = Button(fenetre,text='Quitter',command=fenetre.quit)
    bouton2.pack(side=RIGHT)
    bouton1 = Button(fenetre,text='Recommencer',command=init_jeu)
    bouton1.pack(side=RIGHT)
    photo=PhotoImage(file="pendu_0.gif")
    image_pendu = Label(canevas, image=photo, border=0)
    image_pendu.place(x=120, y=140)
    lettres = canevas.create_text(320,60,text="",fill='black',font='Courrier 30') 
    felicitation=canevas.create_text(320,110,text="",fill='green',font='Courrier 40')
    init_jeu()
    fenetre.mainloop()
    fenetre.destroy()
    
if fich==3:
    def lettre_dans_mot(lettre):
        global partie_en_cours, mot_partiel, mot_choisi, nb_echecs, image_pendu
        global bouton, felicitation
        if partie_en_cours : 
            nouveau_mot_partiel = ""
            lettre_dans_mot = False
            i=0
            while i<len(mot_choisi):
                if mot_choisi[i]==lettre:
                    nouveau_mot_partiel = nouveau_mot_partiel + lettre
                    lettre_dans_mot = True 
                else:
                    nouveau_mot_partiel = nouveau_mot_partiel + mot_partiel[i]
                i+=1
            mot_partiel = nouveau_mot_partiel  
            afficher_mot(mot_partiel)
            bouton[ord(lettre)-65].config(text="  ")
            if not lettre_dans_mot :        # lettre fausse. Changer le dessin.
                nb_echecs += 1
                nomFichier = "pendu_"+str(nb_echecs)+".gif"
                photo=PhotoImage(file=nomFichier)
                image_pendu.config(image=photo)
                image_pendu.image=photo
                if nb_echecs == 7:  # trop d'erreurs. Fini.
                    partie_en_cours = False
                    afficher_mot(mot_choisi)
            elif mot_partiel == mot_choisi:  # le mot a était trouvée !
                partie_en_cours = False
                canevas.itemconfig(felicitation,text="bravo " + prenom +" !")
    def afficher_mot(mot):
        global lettres
        mot_large = ""
        i=0
        while i<len(mot):  # ajoute un espace entre les lettres
            mot_large = mot_large + mot[i] + " "
            i+=1
        canevas.delete(lettres)
        lettres = canevas.create_text(320,60,text=mot_large,fill='black',font='Courrier 30')
    def init_jeu():
        global mot_choisi, mot_partiel, image_pendu, lettres, nb_echecs
        global partie_en_cours, liste_f, bouton, felicitation
        for i in range(26):
            bouton[i].config(text=chr(i+65))
        nb_echecs = 0
        partie_en_cours = True
        mot_choisi = choice(liste_f).rstrip()
        mot_partiel = "-" * len(mot_choisi)
        afficher_mot(mot_partiel)
        photo=PhotoImage(file="pendu_0.gif")
        image_pendu.config(image=photo)
        image_pendu.image=photo
        canevas.itemconfig(felicitation,text="")
    fenetre = Tk()
    fenetre.title("Le jeu du pendu par Thibaut, Marie et Maxence.")
    canevas = Canvas(fenetre, bg='white', height=500, width=620)
    canevas.pack(side=BOTTOM)
    bouton = [0]*26
    for i in range(26):
        bouton[i] = Button(fenetre,text=chr(i+65),command=lambda x=i+65:lettre_dans_mot(chr(x)))
        bouton[i].pack(side=LEFT)
    bouton2 = Button(fenetre,text='Quitter',command=fenetre.quit)
    bouton2.pack(side=RIGHT)
    bouton1 = Button(fenetre,text='Recommencer',command=init_jeu)
    bouton1.pack(side=RIGHT)
    photo=PhotoImage(file="pendu_0.gif")
    image_pendu = Label(canevas, image=photo, border=0)
    image_pendu.place(x=120, y=140)
    lettres = canevas.create_text(320,60,text="",fill='black',font='Courrier 30') 
    felicitation=canevas.create_text(320,110,text="",fill='green',font='Courrier 40')
    init_jeu()
    fenetre.mainloop()
    fenetre.destroy()


EDIT: Ajout de la coloration syntaxique.
A voir également:

3 réponses

dsy73 Messages postés 9252 Date d'inscription dimanche 22 août 2010 Statut Contributeur Dernière intervention 23 octobre 2020 2 476
24 mai 2015 à 04:15
Salut

Quelle est l'erreur ?
Qu'est-ce qui "ne marche pas" ?
0
Le programme m'annonce une erreure qui dit que init_jeu n'est pas défini ..
0
C'est écrit :
"Traceback (most recent call last):
File "C:\Users\Marie\Desktop\Alpha test\ess3.py", line 93, in <module>
bouton1 = Button(fenetre,text='Recommencer',command=init_jeu)
NameError: name 'init_jeu' is not defined"
0
dsy73 Messages postés 9252 Date d'inscription dimanche 22 août 2010 Statut Contributeur Dernière intervention 23 octobre 2020 2 476 > Marie27300
24 mai 2015 à 13:05
il va être difficile de t'aider car la ligne 93 ne correspond pas au code fourni.
0
Ici il est a la ligne 84, j'ai du mettre des espaces désolée
0
Et si je met des espaces de la ligne 74 à 93 comme j'ai fais dans les deux autres boucles pour fich==2 et fich==3 alors ça m'affiche : name "init_jeu" is not défined" :(
0