Probleme de variables Global

Fermé
Snake7791 Messages postés 1 Date d'inscription lundi 15 janvier 2018 Statut Membre Dernière intervention 15 janvier 2018 - Modifié le 16 janv. 2018 à 15:33
 Lepython - 4 févr. 2018 à 19:29
Bonjour, je dois rendre un projet de pendu mais j'ai un probleme je n'arrive pas a définir des variable global lorsque que je met des valeurs en global dans ma premiere fonction , elle n'apparaisse pas en global dans la deuxieme fonction et python me met comme message d'erreur "UnboundLocalError: local variable 'nom de la variable' referenced before assignment"
Pouvez-vous m'aider a comprendre pourquoi les variables reste local svp ?




from tkinter import *
from tkinter.messagebox import *
import time





def IntroMot():
    global m,n,z,a,k,tab,L,Lt,rangs,q
     
    m= ei.get()         #Mot du joueur 1 
    n= len(m)
    z=0                 #Quand joueur 2 donne une bonne lettre
    a=0                 #Avancement du bonhomme
    
    k=str("_ "*n)
    tab=list(k)
    L=["-"]
    Lt=str("")
    rangs = []
    
    q=[]
    MOTS= open("MOTS.txt", "r")
    p=0                #Mot pas du dico (de base)
    
    for ligne in MOTS:
        if m in ligne:
            if n== (len(ligne)-1):
                p=1
                

    if p==0:            #Si pas du dico NOPE
        showerror("ERREUR","Soyez sympathique ! Donnez un mot qui existe au Joueur 2...")

    if p==1:
        #bouton continuer
        bouton2= Button(intro, text="Continuer le jeu ", command= intro.destroy)
        bouton2.place(x=680, y=350)
        
        


def fenetre():
    print(m,n)
    z=0                 #Quand joueur 2 donne une bonne lettre
    a=0                 #Avancement du bonhomme
    
    k=str("_ "*n)
    tab=list(k)
    L=["-"]
    Lt=str("")
    rangs = []
    
    q=[]
    l = ch.get()
    
    q.append (l+",")
    M="".join(q)
    introduction3 = Label(fen1, text="Le mot recherché ! :"+k)
    introduction3.place(x=450, y=155)
    if not (l==""):
        showinfo("ok","Lettres utilisées :"+M)

    if a< 7:
   
           
            lt="".join(L)
            
            
            
            L.append(l)
            print("L=",L)
            for rang in range(len(m)):  # La variable rang parcourt les entiers de 0 au rang de la dernière lettre du mot.
                print(m)
                if m[rang] == l:
                    rangs.append(rang)
                    print(rangs)
                    
            for i in lt:
                if i==l:
                    print("Deja demandez !")
                elif i!=l:


                    
                   
                            
                            for j in m:
                                if j==l:
                                   
                                    
                                    for h in rangs:
                                        N=h*2
                                        
                                        tab[N]=l
                                        k="".join(tab)
    
    
    if a < 7:
        
        print(len(rangs))
        if z==z+len(rangs):
            a=a+1
        
            if a==1:
                droite=c.create_line(152,300,250,400,width=3)
            if a==2:
                droite=c.create_line(152,300,200,400,width=3)
                droite=c.create_line(100,400,148,300,width=3)
            if a==3:
                droite=c.create_line(152,300,200,400,width=3)
                droite=c.create_line(100,400,148,300,width=3)
                droite=c.create_line(150,200,150,290,width=3)
            if a==4:
                droite=c.create_line(152,300,200,400,width=3)
                droite=c.create_line(100,400,148,300,width=3)
                droite=c.create_line(150,200,150,290,width=3)
                droite=c.create_line(155,200,200,290,width=3)

            if a==5:
                droite=c.create_line(152,300,200,400,width=3)
                droite=c.create_line(100,400,148,300,width=3)
                droite=c.create_line(150,200,150,290,width=3)
                droite=c.create_line(155,200,200,290,width=3)
                droite=c.create_line(100,290,145,200,width=3)
            if a==6:
                droite=c.create_line(152,300,200,400,width=3)
                droite=c.create_line(100,400,148,300,width=3)
                droite=c.create_line(150,200,150,290,width=3)
                droite=c.create_line(155,200,200,290,width=3)
                droite=c.create_line(100,290,145,200,width=3)
                droite=c.create_line(150,190,150,160,width=3)

            if a==7:
                droite=c.create_line(152,300,200,400,width=3)
                droite=c.create_line(100,400,148,300,width=3)
                droite=c.create_line(150,200,150,290,width=3)
                droite=c.create_line(155,200,200,290,width=3)
                droite=c.create_line(100,290,145,200,width=3)
                droite=c.create_line(150,190,150,160,width=3)
                cercle=c.create_oval(130,100,170,155,width=3)
            
        else:
            
            z=z+len(rangs)
            print("z=",z)
        rangs = []

   
    
        
        
        
        
                                        
                                
    if z+1==n:
        showinfo("Gagné","le mot a trouvé etait :"+k)
    
    


intro= Tk()
intro.geometry("1000x650")
intro.title("Bienvenue dans le jeu du Pendu")
intro.resizable(False,False)

#ei pour Entree Intro (Champ de texte)
ei= Entry(intro, width=40)
ei.focus_set()
ei.place(x=380, y=295)

#titre au dessus de ei
eiT= Label(intro, text="Joueur 1, entrez un mot.")
eiT.place(x=440, y=260)

#bouton confirmer 1
bouton1= Button(intro, text="Confirmer", command= IntroMot)  #Le bouton fait la commande IntroMot de là haut
bouton1.place(x=680, y=290)


#bouton fermer 2
bouton2= Button(intro, text="Quitter le jeu", command= intro.destroy)
bouton2.place(x=900, y=20)
    
intro.mainloop()




#fenetre 2
fen1 = Tk()
fen1.geometry("1000x650") 
fen1.title("Le jeu du pendu")

c=Canvas (fen1,width=300,height=500,bg="green")
c.place(x=50,y=80)

introduction = Label(fen1, text="Entrez une lettre ! :")
introduction.place(x=560, y=440)

ch = Entry(fen1,bg ='white', fg='black')
ch.focus_set()
ch.place(x=675, y=440)

introduction2 = Label(fen1, text="Le mot recherché ! :")
introduction2.place(x=450, y=155)


    
bouton1 = Button(fen1, text = "Valider", command = fenetre)
bouton1.place(x=800,y=435)


fen1.mainloop()                     
                        




A voir également:

2 réponses

quent217 Messages postés 421 Date d'inscription vendredi 25 septembre 2015 Statut Membre Dernière intervention 1 mars 2024 344
3 févr. 2018 à 19:08
Bonjour,
lorsque tu écrit global dans une fonction ça permet de récupérer des variables qui ont été déclarés en dehors de la fonction comme si elles étaient globales. Si tu veux que les variables soient accessibles depuis les 2 fonctions, il faut les déclarer global dans le code principale (et peut être aussi dans la deuxième fonction).
0
Il faut déclarer les variables globales aussi dans la deuxième fonction
0