Partage
  • Partager sur Facebook
  • Partager sur Twitter

Pendu python

ISN

    13 mai 2015 à 14:16:54

    Bonjour,

    Nous sommes en classe de terminale et avons choisi l'option ISN, notre projet est de faire un quizz-pendu

    Le but de notre projet est de générer des questions aléatoires où le joueur y répondra sous forme de pendu.

    Notre problème est que lorsque la lettre n'est pas présente dans le mot, le compteur n'ajoute pas +1

    Notre second problème et que si la lettre est présente deux fois dans le mot , seule la première s'affiche.

    Restera le problème de générer une nouvelle question lorsque le joueur aura trouvé le mot

    Merci de bien vouloir nous aider

    Voici notre code :

    from tkinter import *
    import random
    
    pendu=0
    nbvrai=0
    pos=0
    
    
    def game_over():
        print("vous avez perdu")
        fenetre.destroy
    
    def position():
        pos=reponse.index(lettre)
    
    def validiteletre():
       (lettre in reponse)
    
    def put_pos():
        pos = reponse.index(lettre)
        nbvrai=+1
        reponse[pos]='lettre'
    def gagner():
        print("vous avez gagné ")
    
    def essai(lettre):
        if validite==True:
            print(lettre)in Label_case+str(pos)
        if validite==False:
            print(lettre)in Label_ltrepropos
    
            pendu=+1
            
            
    
    
    def Btn_Action_A():
        if "A" in reponse:
            global pos
            pos=reponse.index("A")
            exec("Label_case"+str(pos)+".config(text='A')")
        else:
            exec("Label_ltrepropos.config(text='A')")
            global pendu
            pendu=pendu+1
    
            
    def Btn_Action_Z():
        if "Z" in reponse:
            global pos
            pos=reponse.index("Z")
            exec("Label_case"+str(pos)+".config(text='Z')")
        else:
            exec("Label_ltrepropos.config(text='Z')")
            global pendu
            pendu=pendu+1
    def Btn_Action_E():
        if "E" in reponse:
            global pos
            pos=reponse.index("E")
            exec("Label_case"+str(pos)+".config(text='E')")
        else:
            exec("Label_ltrepropos.config(text='E')")
            global pendu
            pendu=pendu+1
            
          
            
       
    
    def Btn_Action_R():
        global pos
        pos=reponse.index("R")
        exec("Label_case"+str(pos)+".config(text='R')")
    
    def Btn_Action_T():
        global pos
        pos=reponse.index("E")
        exec("Label_case"+str(pos)+".config(text='E')")
    
    def Btn_Action_Y():
        global pos
        pos=reponse.index("Y")
        exec("Label_case"+str(pos)+".config(text='Y')")
    
    def Btn_Action_U():
        global pos
        pos=reponse.index("U")
        exec("Label_case"+str(pos)+".config(text='U')")
    
    def Btn_Action_I():
        global pos
        pos=reponse.index("I")
        exec("Label_case"+str(pos)+".config(text='I')")
    
    def Btn_Action_O():
        global pos
        pos=reponse.index("O")
        exec("Label_case"+str(pos)+".config(text='O')")
    
    def Btn_Action_P():
        global pos
        pos=reponse.index("P")
        exec("Label_case"+str(pos)+".config(text='P')")
    
    def Btn_Action_Q():
        global pos
        pos=reponse.index("Q")
        exec("Label_case"+str(pos)+".config(text='Q')")
    
    
    def Btn_Action_S():
        global pos
        pos=reponse.index("S")
        exec("Label_case"+str(pos)+".config(text='S')")
    
    
    def Btn_Action_D():
        global pos
        pos=reponse.index("D")
        exec("Label_case"+str(pos)+".config(text='D')")
    
    
    def Btn_Action_F():
        global pos
        pos=reponse.index("F")
        exec("Label_case"+str(pos)+".config(text='F')")
    
    
    def Btn_Action_G():
        global pos
        pos=reponse.index("G")
        exec("Label_case"+str(pos)+".config(text='G')")
    
    
    def Btn_Action_H():
        global pos
        pos=reponse.index("H")
        exec("Label_case"+str(pos)+".config(text='H')")
    
    
    def Btn_Action_J():
        global pos
        pos=reponse.index("J")
        exec("Label_case"+str(pos)+".config(text='J')")
    
    
    def Btn_Action_K():
        global pos
        pos=reponse.index("L")
        exec("Label_case"+str(pos)+".config(text='L')")
    
    
    def Btn_Action_L():
        global pos
        pos=reponse.index("L")
        exec("Label_case"+str(pos)+".config(text='L')")
    
    
    def Btn_Action_M():
        global pos
        pos=reponse.index("M")
        exec("Label_case"+str(pos)+".config(text='M')")
    
    
    def Btn_Action_W():
        global pos
        pos=reponse.index("W")
        exec("Label_case"+str(pos)+".config(text='W')")
    
    
    def Btn_Action_X():
        global pos
        pos=reponse.index("X")
        exec("Label_case"+str(pos)+".config(text='X')")
    
    
    def Btn_Action_C():
        global pos
        pos=reponse.index("C")
        exec("Label_case"+str(pos)+".config(text='C')")
    
    
    def Btn_Action_V():
        global pos
        pos=reponse.index("V")
        exec("Label_case"+str(pos)+".config(text='V')")
    
    
    def Btn_Action_B():
        global pos
        pos=reponse.index("B")
        exec("Label_case"+str(pos)+".config(text='B')")
    
    
    def Btn_Action_N():
        global pos
        pos=reponse.index("N")
        exec("Label_case"+str(pos)+".config(text='N')")
    
    
    
    Fenetre = Tk()
    Fenetre.title('PENDU')
    Fenetre.geometry('1240x960')
    
    
    
    nbpresence=0
    theme=['GOT','BB']
    # ici les themes sont définis 
    questionGOT=['Quel est le nom de la famille pricipale dans GOT?','Quel animal represente la famille Stark','question3','question4']
    reponseGOT=[['S','T','A','R','K'],['L','O','U','P'],['R','E','P','O','N','S','E','3'],['R','E','P','O','N','S','E','4']]
    # les questions et les réponses du 1 er theme sont definis 
    questionBB=['Quel est le surnom de W.White dans la série Breaking Bad?', 'Couleur du ciel?']
    reponseBB=[['H','E','I','S','E','N','B','E','R','G'],['B','L','E','U']]
    # les questions et les réponses du 2eme theme sont definis 
    
    theme_choisi=random.choice(theme)
    #print('theme',theme_choisi)
    # un theme est choisi au hasard et est afficher à l'utilisateur 
    if theme_choisi == 'GOT':
        question_choisie=random.choice(questionGOT)
        (questionGOT.index(question_choisie))
        reponse=(reponseGOT[questionGOT.index(question_choisie)])
    #choix au hasard d'une question en fonction du theme choisi précédemment  
    # en fonction du theme et de la question choisi, la réponse est definie
    
    
    elif theme_choisi == 'BB' :
        question_choisie=random.choice(questionBB)
        (questionBB.index(question_choisie))
        reponse=(reponseBB[questionBB.index(question_choisie)])
    #choix au hasard d'une question en fonction du theme choisi précédemment  
    # en fonction du theme et de la question choisi, la réponse est definie    
    print('question choisie :',question_choisie)
    #affichage de la question 
    
    
    # la réponse est directement convertie en majuscule pour éviter les mauvaises réponse dus a une minuscule ou un accent 
    print('taille reponse',len(reponse))
    print (reponse)
    
    def test(lettre):
        global pendu, flag_game
        lettre=lettre.upper()
        validite()
        
        if validite == True:
            put_pos()
            if nbvrai==len(reponse):
                gagner()
               
           
            
            
    
        if validite == False:
            pendu+=1
            if pendu >4:
                print(game_over)
        
    
    
    rep=len(reponse)
    xpos=100
    ypos=500
    pas=100
    for loop in range(rep):
        exec("Label_case"+str(loop)+"=Label(Fenetre,bg='yellow',width=7)")
        Label_case=Label(Fenetre,bg='yellow',width=15)
        xpos=xpos+pas
        exec('Label_case'+str(loop)+'.place(x='+str(xpos)+',y='+str(ypos)+')')
        
        
    
    
    
    
    
    
    Label_theme=Label(Fenetre, text=theme_choisi)
    Label_theme.place(x=50,y=100)
    
    Label_question=Label(Fenetre,text=question_choisie)
    Label_question.place(x=50,y=200)
    
    Label_pendu=Label(Fenetre, text=pendu)
    Label_pendu.place(x=1000,y=100)
    
    Label_gagner=Label(Fenetre)
    Label_gagner.place(x=300,y=300)
    
    Label_perdu=Label(Fenetre)
    Label_perdu.place(x=300,y=300)
    
    Label_ltrepropos=Label(Fenetre,bg='green',width=15,height=10)
    Label_ltrepropos.place(x=1000,y=200)
    
    bouton_A=Button(Fenetre, text="A",width=15,height=2, command=Btn_Action_A)
    bouton_A.place(x=100,y=800)
    
    bouton_Z=Button(Fenetre, text="Z",width=15,height=2,command=Btn_Action_Z )
    bouton_Z.place(x=200,y=800)
    
    bouton_E=Button(Fenetre, text="E", width=15,height=2,command=Btn_Action_E)
    bouton_E.place(x=300,y=800)
    
    bouton_R=Button(Fenetre, text="R", width=15,height=2,command=Btn_Action_R)
    bouton_R.place(x=400,y=800)
    
    bouton_T=Button(Fenetre, text="T",width=15,height=2,command=Btn_Action_T)
    bouton_T.place(x=500,y=800)
    
    bouton_Y=Button(Fenetre, text="Y", width=15,height=2,command=Btn_Action_Y)
    bouton_Y.place(x=600,y=800)
    
    bouton_U=Button(Fenetre, text="U",width=15,height=2,command=Btn_Action_U)
    bouton_U.place(x=700,y=800)
    
    bouton_I=Button(Fenetre, text="I",width=15,height=2,command=Btn_Action_I)
    bouton_I.place(x=800,y=800)
    
    bouton_O=Button(Fenetre, text="O", width=15,height=2,command=Btn_Action_O)
    bouton_O.place(x=900,y=800)
    
    
    bouton_P=Button(Fenetre, text="P", width=15,height=2,command=Btn_Action_P)
    bouton_P.place(x=1000,y=800)
    #-------------------------------------------
    bouton_Q=Button(Fenetre, text="Q", width=15,height=2,command=Btn_Action_Q )
    bouton_Q.place(x=100,y=850)
    
    bouton_S=Button(Fenetre, text="S", width=15,height=2,command=Btn_Action_S )
    bouton_S.place(x=200,y=850)
    
    bouton_D=Button(Fenetre, text="D", width=15,height=2,command=Btn_Action_D )
    bouton_D.place(x=300,y=850)
    
    bouton_F=Button(Fenetre, text="F", width=15,height=2,command=Btn_Action_F )
    bouton_F.place(x=400,y=850)
    
    bouton_G=Button(Fenetre, text="G", width=15,height=2,command=Btn_Action_G )
    bouton_G.place(x=500,y=850)
    
    bouton_H=Button(Fenetre, text="H", width=15,height=2,command=Btn_Action_H)
    bouton_H.place(x=600,y=850)
    
    bouton_J=Button(Fenetre, text="J", width=15,height=2,command=Btn_Action_J )
    bouton_J.place(x=700,y=850)
    
    bouton_K=Button(Fenetre, text="K", width=15,height=2,command=Btn_Action_K )
    bouton_K.place(x=800,y=850)
    
    bouton_L=Button(Fenetre, text="L", width=15,height=2,command=Btn_Action_L )
    bouton_L.place(x=900,y=850)
    
    bouton_M=Button(Fenetre, text="M", width=15,height=2,command=Btn_Action_M )
    bouton_M.place(x=1000,y=850)
    #.........................................
    
    bouton_W=Button(Fenetre, text="W", width=15,height=2,command=Btn_Action_W )
    bouton_W.place(x=250,y=900)
    
    bouton_X=Button(Fenetre, text="X", width=15,height=2,command=Btn_Action_X )
    bouton_X.place(x=350,y=900)
    
    bouton_C=Button(Fenetre, text="C",width=15,height=2,command=Btn_Action_C  )
    bouton_C.place(x=450,y=900)
    
    bouton_V=Button(Fenetre, text="V", width=15,height=2,command=Btn_Action_V )
    bouton_V.place(x=550,y=900)
    
    bouton_B=Button(Fenetre, text="B",width=15,height=2,command=Btn_Action_B )
    bouton_B.place(x=650,y=900)
    
    bouton_N=Button(Fenetre, text="N", width=15,height=2,command=Btn_Action_N )
    bouton_N.place(x=750,y=900)
    
    bouton=Button(Fenetre, text="quitter", command=Fenetre.destroy)
    bouton.place(x=10,y=10)
    
    
    Fenetre.mainloop()
    




    • Partager sur Facebook
    • Partager sur Twitter
      13 mai 2015 à 14:24:07

      Pour le compteur (ligne 32)
      #pendu =+ 1
      #s'ecrit en fait
      
      pendu += 1
      Pour le reste j'éditerais mon post ...
      En tout cas c'est très moche ... Pourquoi ne pas faire une fonction Btn_Action avec en parametre la lettre au lieu de 26 fonctions ?
      def Btn_Action(lettre):
        global pos
        pos=reponse.index(lettre)
        exec("Label_case"+str(pos)+".config(text='"+lettre+"')")
      
      bouton_R = Button(Fenetre, text="R", width = 15,height=2,command=Btn_Action("R"))

      -
      Edité par IQbrod 13 mai 2015 à 14:28:35

      • Partager sur Facebook
      • Partager sur Twitter
      OpenClassrooms retire tellement d'aiguilles de nos pieds qu'on pourrait ne plus trouver le foin de notre botte :)
        14 mai 2015 à 9:32:29

        Bonjour,

        Merci d'avoir répondu.

        Nous savons que 26 fonctions n'est pas très pratique ni esthétique.

        Malheureusement même avec pendu += 1 notre compteur de varie pas.

        J'ai essayé de définir la fonction Btn_Action mais lorsque je lance le programme une erreur s'affiche et me dit que Fenetre n'est pas définie alors que je l'ai bien écrit

        • Partager sur Facebook
        • Partager sur Twitter
        Anonyme
          14 mai 2015 à 9:46:09

          Il faut tout recommencer et avec une bonne base sans tkinter...

          Tu peux utiliser les bytearray, pratique pour modifier en live ta chaîne de caractères

          def rep(secret, chaine, caractere):
              for ind, car in enumerate(secret):
                  if car == caractere:
                      chaine[ind] = ord(car)
          
          SECRET = 'bonjour'
          chaine = bytearray('*******', 'utf8')
          rep(SECRET, chaine, 'o')
          print(chaine) # bytearray(b'*o**o**')

          Évidemment on peut faire sans les bytearray, mais je trouve que pour les débutants, ça vire l'intention d'utiliser le mot clé global à mauvais escient !

          EDIT: Ça prend 10 minutes pour faire un pendu digne de ce nom sans tkinter, 1 heure à votre niveau, ça vaut le coup de le refaire nickel pour travailler avec tkinter par la suite...

          -
          Edité par Anonyme 14 mai 2015 à 9:52:13

          • Partager sur Facebook
          • Partager sur Twitter

          Pendu python

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown