Code calcul bac

Fermé
Itachi-Uchiha - 10 déc. 2014 à 18:28
 Itachi-Uchiha - 10 déc. 2014 à 22:20
Bonjour,
j'ai réaliser un programme pour calculer la note du BAC mais celui-ci plante à la derniere étape (fonction calcul_note):
from Tkinter import *
import tkMessageBox
   
window = Tk()
  
note = IntVar()
bac = StringVar()
  
numero = 0 
notes = []
   
def choisir_bac():
    global matieres, coeffs, notes, app
    global question, saisie
     
    bac_var = bac.get()
  
    if bac_var == "S":
        matieres = ['Maths','PC','LV1', 'Fr_Ecrit', 'Fr_Oral', 'HG', \
                    'SI', 'ISN', 'LV2', 'Philo', 'EPS', 'TPE']
        coeffs = {'Maths':7, 'PC':6,'LV1':3, 'Fr_Ecrit':2, 'Fr_Oral':2, \
                  'HG':3, 'SI':6, 'ISN':2, 'LV2':2, 'Philo':3, 'EPS':2, \
                  'TPE':2}
    elif bac_var == "ES":
        matieres = ['Maths','Sciences','LV1', 'Fr_Ecrit', 'Fr_Oral', 'HG', \
                    'Sc.Eco', 'Sc.Pol.Sociale', 'LV2', 'Philo', 'EPS', 'TPE']
        coeffs = {'Maths':5, 'Sciences':2,'LV1':3, 'Fr_Ecrit':2, 'Fr_Oral':2, \
                  'HG':5, 'SC.Eco':7, 'Sc.Pol.Sociale':4, 'LV2':2, 'Philo':4, \
                  'EPS':2, 'TPE':2}

    app.destroy()
    app = Frame(window)
    app.pack()
    question = Label(app, text = "Entrez votre note de " + matieres[numero])
    question.pack()
    saisie = Entry(app, textvariable = note, width = 10)
    saisie.pack()
    saisie.delete(0, END)
    saisie.focus()
    bouton_ok = Button(app, text = "OK", command = ajoute_note)
    bouton_ok.pack(side = BOTTOM)
     
def ajout_notes():
    global numero
    # Validation
    if 0 <= note.get() <= 20:
        notes.append(note.get())
    else:
        saisie.delete(0, END)
        tkMessageBox.showwarning("Erreur", "Entrez une note de 0 à 20")
        return
    if len(notes) < len(matieres):
        numero += 1
        saisie.delete(0, END)
        question.config(text = "Entrez votre note de " + matieres[numero])
    else:
        bouton_ok = Button(app, text = "OK", command = calcul_note)
        bouton_ok.pack(side = BOTTOM)

def calcul_note():
    m = 0
    for i in len(notes):
        m = m + notes[i] * coeffs[i]
    m = m / len(notes)
    app.destroy()
    app = Frame(window)
    app.pack()
    resultat["text"] = "Voici votre note: {0]".format(m)
    bouton_fin = Button(app, text = "Quitter", command = window.destroy)
    bouton_fin.pack(side = BOTTOM)

app = Frame(window)
app.pack()
intro = Label(app, text = "Ce programme va vous permettre \
de calculer votre resultat au BAC ")
intro.pack()
   
choix = Label(app, text = "Quel bac avez vous passé ?")
choix.pack()
   
bac_S = Radiobutton(app, text="S", variable = bac, value="S")
bac_ES = Radiobutton(app, text="ES", variable = bac, value="ES")
  
bac_S.pack()
bac_ES.pack()
 
bac.set('S') # Initialisation
  
bouton_ok = Button(app, text = "OK", command = choisir_bac)
bouton_ok.pack(side = BOTTOM)
   
window.mainloop()

A voir également:

2 réponses

ghFrankfurt Messages postés 207 Date d'inscription mardi 16 novembre 2010 Statut Membre Dernière intervention 10 décembre 2014 23
10 déc. 2014 à 20:02
Bonjour,

Dans la fonction "ajout_notes()" la liste notes[] n'est pas déclarée global.
Je suppose que ta fonction calcul_note() plante car du coup fait une une division par 0
0
Itachi-Uchiha
10 déc. 2014 à 20:39
Oui mais elle est déclarée comme telle dans la fonction choisir_bac().
0
ghFrankfurt Messages postés 207 Date d'inscription mardi 16 novembre 2010 Statut Membre Dernière intervention 10 décembre 2014 23
Modifié par ghFrankfurt le 10/12/2014 à 21:11
Effectivement...

En revanche to for est faux.

for i in len(notes):
Pour récupérer les index tu dois faire for i in range(len(notes)):

Egalement, si je regarde un peu plus haut, coeff est un dictionnaire et les dictionnaires ne sont pas ordonnés comme les listes. Leurs unique index sont ceux qui sont défini : "Math, PC, LV1, ..."
0
Itachi-Uchiha
10 déc. 2014 à 21:34
for i in len(notes):
Pour récupérer les index tu dois faire for i in range(len(notes)):

Je pensais que ça suffisait mais ok j'ai modifié
Mais maintenant on m'indique une erreur sur cette ligne

m = m + notes[i] * coeffs[i]


KeyError: 0
Qu'est ce que ça signifie ?
0
ghFrankfurt Messages postés 207 Date d'inscription mardi 16 novembre 2010 Statut Membre Dernière intervention 10 décembre 2014 23 > Itachi-Uchiha
10 déc. 2014 à 21:41
Comme je disais, "coeff" est un dictionnaire. Tu ne peux pas récupérer les valeurs comme dans une liste.
Tu dois récupérer les valeurs par leur index coeffs['Maths'] coeffs['PC']

coeffs[0] ..[1] ...[2] .. n'existent pas.
0
Itachi-Uchiha
10 déc. 2014 à 22:20
Regarde ce code et teste le psk je vois pas l'erreur et tu cerneras mieux mon problème je pense:
from Tkinter import *
import tkMessageBox
   
window = Tk()
c = Canvas(window)
c.pack()
# image
fond = PhotoImage(file="forme.gif")
#image de fond 
c.create_image(0,0, image=fond)
  
  
note = IntVar()
bac = StringVar()
  
numero = 0 
notes = []
   
def choix_bac():
    global matieres, coeffs, app, question, saisie
    bac_var = bac.get()
    if bac_var == "S":
        matieres = ['Maths','PC','LV1', 'Fr_Ecrit', 'Fr_Oral', 'HG', \
                    'SI', 'ISN', 'LV2', 'Philo', 'EPS', 'TPE']
        coeffs = [7, 6, 3, 2, 2, 3, 6, 2, 2, 3, 2, 2]
    elif bac_var == "ES":
        matieres = ['Maths','Sciences','LV1', 'Fr_Ecrit', 'Fr_Oral', 'HG', \
                    'Sc.Eco', 'Sc.Pol.Sociale', 'LV2', 'Philo', 'EPS', 'TPE']
        coeffs = [5, 2,3, 2, 2, 5, 7, 4, 2, 4, 2, 2]

    app.destroy()
    app = Frame(window)
    app.pack()
    question = Label(app, text = "Entrez votre note de " + matieres[numero])
    question.pack()
    saisie = Entry(app, textvariable = note, width = 10)
    saisie.pack()
    saisie.delete(0, END)
    saisie.focus()
    bouton_ok = Button(app, text = "OK", command = ajout_notes)
    bouton_ok.pack(side = BOTTOM)
     
def ajout_notes():
    global numero
    # Validation
    if 0 <= note.get() <= 20:
        notes.append(note.get())
    else:
        saisie.delete(0, END)
        tkMessageBox.showwarning("Erreur", "Entrez une note de 0 à 20")
        return
    if len(notes) < len(matieres):
        numero += 1
        saisie.delete(0, END)
        question.config(text = "Entrez votre note de " + matieres[numero])
    else:
        bouton_ok = Button(app, text = "OK", command = calcul_note)
        bouton_ok.pack(side = BOTTOM)

def calcul_note():
    m = 0
    for i in range(len(notes)):
        m = m + notes[i] * coeffs[i]
    m = m / len(notes)
    app = Frame(window)
    app.pack()
    resultat = Label(app, text = "Voici votre note:{0}".format(m))
    resultat.pack()
    bouton_fin = Button(app, text = "Quitter", command = window.destroy)
    bouton_fin.pack(side = BOTTOM)

app = Frame(window)
app.pack()
intro = Label(app, text = "Ce programme va vous permettre \
de calculer votre resultat au BAC ")
intro.pack()
   
choix = Label(app, text = "Quel bac avez vous passé ?")
choix.pack()
   
bac_S = Radiobutton(app, text="S", variable = bac, value="S")
bac_ES = Radiobutton(app, text="ES", variable = bac, value="ES")
  
bac_S.pack()
bac_ES.pack()
 
bac.set('S') # Initialisation
  
bouton_ok = Button(app, text = "OK", command = choix_bac)
bouton_ok.pack(side = BOTTOM)
   
window.mainloop()

0