Erreur dans mon programme
Phil_1857 Messages postés 1872 Date d'inscription Statut Membre Dernière intervention -
Est ce que quelqu'un peux m'aider, je dois faire un programme qui demande à l'utilisateur de rentrer une fonction polynome du second degré, qui lui affiches ses racines et la courbe représentative de cette fonction. Il y a une erreur, pourriez vous m'aider à la régler :
Traceback (most recent call last):
File "C:\Users\Desktop\Polynome du second degré.py", line 76, in <module>
y = f(x)
File "C:\Users\Desktop\Polynome du second degré.py", line 73, in f
return a * x**2 + b * x + c
NameError: name 'a' is not defined
from tkinter import * from math import * import matplotlib.pyplot as plt import numpy as np # Création et personalisation de la fenêtre fenetre = Tk() fenetre.title("Polynome du second degré") fenetre.geometry("662x400") fenetre.config(background="#1E1E70") # Le titre titre = Label(fenetre, text="Entrez la fonction :",font=("Courrier", 19), bg="#1E1E70", fg="white") titre.place(x=234,y=50) # Les entrees de l'utilisateur entree_a = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_a.place(x=245,y=95) entree_b = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_b.place(x=320,y=95) entree_c = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_c.place(x=387,y=95) def résolution_eq(): a = float(entree_a.get().strip()) b = float(entree_b.get().strip()) c = float(entree_c.get().strip()) delta = b**2-4*a*c #Affichage des solutions if delta < 0 : solution1 = Label(fenetre, text=f" Δ = {delta} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution1.place(x=200,y=205) solution1_1 = Label(fenetre, text=f" donc l'équation {a}x²+{b}x+{c} = 0 ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution1_1.place(x=88,y=240) solution1_2 = Label(fenetre, text=" ne possède pas de solution ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution1_2.place(x=94,y=275) elif delta == 0 : x0 = round((-b)/2*a,2) solution2 = Label(fenetre, text=" Δ = 0 ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution2.place(x=200,y=205) solution2_1 = Label(fenetre, text=f"donc la solution unique de l'équation {a}x²+{b}x+{c} = 0 ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution2_1.place(x=24,y=240) solution2_2 = Label(fenetre, text=f" est x0 = {x0} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution2_2.place(x=155,y=275) elif delta > 0 : x1 = round(((-b)-sqrt(delta))/(2*a),2) x2 = round(((-b)+sqrt(delta))/(2*a),2) solution3 = Label(fenetre, text=f" Δ = {delta} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution3.place(x=200,y=205) solution3_1 = Label(fenetre, text=f" donc l'équation {a}x²+{b}x+{c} = 0 possède ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution3_1.place(x=24,y=240) solution3_2 = Label(fenetre, text=f" deux solutions : x1 = {x1}, et x2 = {x2} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution3_2.place(x=18,y=275) # Le texte de la fonction fonction0 = Label(fenetre, text="x²+",font=("Courrier", 19), bg="#1E1E70", fg="white") fonction0.place(x=282,y=89) fonction0_1 = Label(fenetre, text="x+",font=("Courrier", 19), bg="#1E1E70", fg="white") fonction0_1.place(x=357,y=89) # Création du bouton résolution Bouton_résolution = Button(fenetre, text = "Résolution de l'équation",font=("Courrier", 16), fg="#161651", command=résolution_eq) Bouton_résolution.place(x=212,y=145) def f(x): return a * x**2 + b * x + c x = np.linspace(-10, 10, 100) y = f(x) fig, ax = plt.subplots() ax.plot(x, y) ax.axhline(0, color='black', lw=1) ax.axvline(0, color='black', lw=1) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_title(f"Courbe représentative de la fonction {a}x²+{b}x+{c}") ax.grid(True) plt.show() # Affichage de la page fenetre.mainloop()
Merci
- Erreur dans mon programme
- Programme demarrage windows - Guide
- Cette action ne peut pas être réalisée car le fichier est ouvert dans un autre programme - Guide
- Mettre en veille un programme - Guide
- Message programmé iphone - Guide
- Forcer la fermeture d'un programme - Guide
5 réponses
Bonjour,
a est définie dans resolution_eq(), elle n'est pas visible dans f()
voir la portée des variables : https://www.pierre-giraud.com/python-apprendre-programmer-cours/portee-variable/
Merci,
Je l'ai corrigé, mais maintenant, il y a un autre probleme :
Traceback (most recent call last):
File "C:\Users\Desktop\Polynome du second degré.py", line 79, in <module>
y = f(x)
File "C:\Users\Desktop\Polynome du second degré.py", line 76, in f
return a*x**2 + b*x+c
numpy.core._exceptions._UFuncNoLoopError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U1'), dtype('float64')) -> None
from tkinter import * from math import * import matplotlib.pyplot as plt import numpy as np # Création et personalisation de la fenêtre fenetre = Tk() fenetre.title("Polynome du second degré") fenetre.geometry("662x400") fenetre.config(background="#1E1E70") # Le titre titre = Label(fenetre, text="Entrez la fonction :",font=("Courrier", 19), bg="#1E1E70", fg="white") titre.place(x=234,y=50) # Les entrees de l'utilisateur entree_a = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_a.place(x=245,y=95) entree_b = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_b.place(x=320,y=95) entree_c = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_c.place(x=387,y=95) def résolution_eq(): a = float(entree_a.get().strip()) b = float(entree_b.get().strip()) c = float(entree_c.get().strip()) delta = b**2-4*a*c #Affichage des solutions if delta < 0 : solution1 = Label(fenetre, text=f" Δ = {delta} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution1.place(x=200,y=205) solution1_1 = Label(fenetre, text=f" donc l'équation {a}x²+{b}x+{c} = 0 ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution1_1.place(x=88,y=240) solution1_2 = Label(fenetre, text=" ne possède pas de solution ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution1_2.place(x=94,y=275) elif delta == 0 : x0 = round((-b)/2*a,2) solution2 = Label(fenetre, text=" Δ = 0 ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution2.place(x=200,y=205) solution2_1 = Label(fenetre, text=f"donc la solution unique de l'équation {a}x²+{b}x+{c} = 0 ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution2_1.place(x=24,y=240) solution2_2 = Label(fenetre, text=f" est x0 = {x0} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution2_2.place(x=155,y=275) elif delta > 0 : x1 = round(((-b)-sqrt(delta))/(2*a),2) x2 = round(((-b)+sqrt(delta))/(2*a),2) solution3 = Label(fenetre, text=f" Δ = {delta} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution3.place(x=200,y=205) solution3_1 = Label(fenetre, text=f" donc l'équation {a}x²+{b}x+{c} = 0 possède ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution3_1.place(x=24,y=240) solution3_2 = Label(fenetre, text=f" deux solutions : x1 = {x1}, et x2 = {x2} ",font=("Courrier", 19), bg="#1E1E70", fg="white") solution3_2.place(x=18,y=275) # Le texte de la fonction fonction0 = Label(fenetre, text="x²+",font=("Courrier", 19), bg="#1E1E70", fg="white") fonction0.place(x=282,y=89) fonction0_1 = Label(fenetre, text="x+",font=("Courrier", 19), bg="#1E1E70", fg="white") fonction0_1.place(x=357,y=89) # Création du bouton résolution Bouton_résolution = Button(fenetre, text = "Résolution de l'équation",font=("Courrier", 16), fg="#161651", command=résolution_eq) Bouton_résolution.place(x=212,y=145) def f(x): a = entree_a.get() b = entree_b.get() c = entree_c.get() return a*x**2 + b*x+c x = np.linspace(-10, 10, 100) y = f(x) fig, ax = plt.subplots() ax.plot(x, y) ax.axhline(0, color='black', lw=1) ax.axvline(0, color='black', lw=1) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_title(f"Courbe représentative de la fonction {a}x²+{b}x+{c}") ax.grid(True) plt.show() # Affichage de la page fenetre.mainloop()
Bonjour,
dans la fonction f, a,b,c sont des string, il faut les convertir avant de les multiplier
De plus tu as mis tes définitions de Labels dans la fonction, donc à chaque fois que tu l'appelle,
tu recrées ces Labels !
from tkinter import * from math import * import matplotlib.pyplot as plt import numpy as np def résolution_eq(): a = float(entree_a.get().strip()) b = float(entree_b.get().strip()) c = float(entree_c.get().strip()) delta = b**2-4*a*c #Affichage des solutions if delta < 0 : solution1['text'] = f" ? = {delta} " solution1_1['text'] = " donc l'équation {a}x²+{b}x+{c} = 0 " solution1_2['text'] = " ne possède pas de solution " elif delta == 0 : #idem elif delta > 0 : #idem def f(x): return a * x**2 + b * x + c fenetre = Tk() fenetre.title("Polynome du second degré") fenetre.geometry("662x400") fenetre.config(background="#1E1E70") titre = Label(fenetre, text="Entrez la fonction :",font=("Courrier", 19), bg="#1E1E70", fg="white") titre.place(x=234,y=50) entree_a = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_a.place(x=245,y=95) entree_b = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_b.place(x=320,y=95) entree_c = Entry(fenetre, font=("Courrier", 15), bg ='white', fg='black', width=3) entree_c.place(x=387,y=95) fonction0 = Label(fenetre, text="x²+",font=("Courrier", 19), bg="#1E1E70", fg="white") fonction0.place(x=282,y=89) fonction0_1 = Label(fenetre, text="x+",font=("Courrier", 19), bg="#1E1E70", fg="white") fonction0_1.place(x=357,y=89) Bouton_résolution = Button(fenetre, text = "Résolution de l'équation",font=("Courrier", 16), fg="#161651", command=résolution_eq) Bouton_résolution.place(x=212,y=145) solution1 = Label(fenetre, text='',font=("Courrier", 19), bg="#1E1E70", fg="white") solution1.place(x=200,y=205) solution1_1 = Label(fenetre, text='',font=("Courrier", 19), bg="#1E1E70", fg="white") solution1_1.place(x=88,y=240) solution1_2 = Label(fenetre, text='',font=("Courrier", 19), bg="#1E1E70", fg="white") solution1_2.place(x=94,y=275) x = np.linspace(-10, 10, 100) y = f(x) fig, ax = plt.subplots() ax.plot(x, y) ax.axhline(0, color='black', lw=1) ax.axvline(0, color='black', lw=1) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_title(f"Courbe représentative de la fonction {a}x²+{b}x+{c}") ax.grid(True) plt.show() fenetre.mainloop()
Comment faut-il que je fasse pour remplacer les Labels par des nouveaux au lieu d'en creer d'autres ?
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionregarde ma réponse précédente, je t'ai affiché le code à jour !
les labels sont créés et placés avec les autres widgets après la création de la fenêtre
et les textes sont mis à jour dans la fonction resolution_eq:
solution1['text'] = f" ? = {delta} "