Python Forum
equation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: equation (/thread-22306.html)



equation - MathisDELAGE - Nov-07-2019

Hello,
I'd like to fix an equation with this program is what it would be possible to please
# coding:utf-8


équation = str(input("saisir une équation: "))

nb_inconnue = int(input("Saisir le nombre d'inconnue: "))

nb_de_caractère1 = int(input("Saissir le nombre de charactère de la premire partie (= exclu): "))
nb_de_caractère2 = int(input("Pareille pour la deuxième partie: "))

nb_de_caractère_totale = nb_de_caractère1 + nb_de_caractère2 + 1

if nb_inconnue == 1:
    nom_inco_1 = str(input("Saisir le nom de l'inconnue 1: "))
elif nb_inconnue == 2:
    nom_inco_1 = str(input("Saisir le nom de l'inconnue 1: "))
    nom_inco_2 = str(input("Saisir le nom de l'inconnue 2: "))
elif nb_inconnue == 3:
    nom_inco_1 = str(input("Saisir le nom de l'inconnue 1: "))
    nom_inco_2 = str(input("Saisir le nom de l'inconnue 2: "))
    nom_inco_3 = str(input("Saisir le nom de l'inconnue 3: "))
elif nb_inconnue == 4:
    nom_inco_1 = str(input("Saisir le nom de l'inconnue 1: "))
    nom_inco_2 = str(input("Saisir le nom de l'inconnue 2: "))
    nom_inco_3 = str(input("Saisir le nom de l'inconnue 3: "))
    nom_inco_4 = str(input("Saisir le nom de l'inconnue 4: "))

équation_part_1 = équation[0: nb_de_caractère1]
équation_part_2 = équation[nb_de_caractère1 + 1: nb_de_caractère_totale]

nb_particulier = str(input("Voulez vous saisir un nombres particiler ?(oui ou non)"))

if nb_particulier == "oui":
    if nb_inconnue == 1:
        nb_choisie1 = float(input("Saisir la valeur de {}: ".format(nom_inco_1)))
        exec(nom_inco_1 + '=' + str(nb_choisie1))
    if nb_inconnue == 2:
        nb_choisie1 = float(input("Saisir la valeur de {}: ".format(nom_inco_1)))
        nb_choisie2 = float(input("Saisir la valeur de {}: ".format(nom_inco_2)))
        exec(nom_inco_1 + '=' + str(nb_choisie1))
        exec(nom_inco_2 + '=' + str(nb_choisie2))
    if nb_inconnue == 3:
        nb_choisie1 = float(input("Saisir la valeur de {}: ".format(nom_inco_1)))
        nb_choisie2 = float(input("Saisir la valeur de {}: ".format(nom_inco_2)))
        nb_choisie3 = float(input("Saisir la valeur de {}: ".format(nom_inco_3)))
        exec(nom_inco_1 + '=' + str(nb_choisie1))
        exec(nom_inco_2 + '=' + str(nb_choisie2))
        exec(nom_inco_3 + '=' + str(nb_choisie3))
    if nb_inconnue == 4:
        nb_choisie1 = float(input("Saisir la valeur de {}: ".format(nom_inco_1)))
        nb_choisie2 = float(input("Saisir la valeur de {}: ".format(nom_inco_2)))
        nb_choisie3 = float(input("Saisir la valeur de {}: ".format(nom_inco_3)))
        nb_choisie4 = float(input("Saisir la valeur de {}: ".format(nom_inco_4)))
        exec(nom_inco_1 + '=' + str(nb_choisie1))
        exec(nom_inco_2 + '=' + str(nb_choisie2))
        exec(nom_inco_3 + '=' + str(nb_choisie3))
        exec(nom_inco_4 + '=' + str(nb_choisie4))
else:
    if nb_inconnue == 1:
        exec(nom_inco_1 + '=' + str(0))
    elif nb_inconnue == 2:
        exec(nom_inco_1 + '=' + str(0))
        exec(nom_inco_2 + '=' + str(0))
    elif nb_inconnue == 3:
        exec(nom_inco_1 + '=' + str(0))
        exec(nom_inco_2 + '=' + str(0))
        exec(nom_inco_3 + '=' + str(0))
    elif nb_inconnue == 4:
        exec(nom_inco_1 + '=' + str(0))
        exec(nom_inco_2 + '=' + str(0))
        exec(nom_inco_3 + '=' + str(0))
        exec(nom_inco_4 + '=' + str(0))

if nb_particulier == "non":
    if nb_inconnue == 1 :
          part1 = eval(équation_part_1)
          part2 = eval(équation_part_2)
         while part1 != part2:
Wall Wall Wall
if you don't understand contact me
thank you in advance


RE: equation - ThomasL - Nov-07-2019

It would help much if you could translate your code into english words as this french code is hard to understand.
And please, what is your question? What is wrong with this code? Do you get errors? Please provide more details.


RE: equation - perfringo - Nov-07-2019

Yes, I don't understand.


RE: equation - jefsummers - Nov-07-2019

A comment before your question - your code does not make efficient use of lists. For example, lines 13 through 26 could be replaced by
nom_inco = []
for idx in range(0,nc_inconnue) :
    nom_temp = input(f"Saisir le nom do l'inconnue {idx}:") #Note that input returns a string. Casting with str() is redundant
    nom_inco.append(nom_temp)
You then address each nom_inco[x] and you know how many there are by nom_inco.len()

Now, your English is better than my French, but it appears you are trying to solve equations in the section 33 to 72. If I am understanding what you are expecting your users to enter, you expect to get a string such as "4*x**2+2*x" or something more complex. In 59 through 72 you set that equal to zero, then want exec() to solve it.

It won't.

exec() will execute a valid Python statement or script. So, your string needs to be something that the Python interpreter will understand. Like:
x = 3
exec("y = 24*x + 17")
print(y)
Output:
89
This works despite y not being defined outside of the exec() function. But, x must be defined for it to work.
So, exec() executes commands, does not evaluate a function.

Is this what you were asking?


RE: equation - ThomasL - Nov-07-2019

Nevertheless, DO NOT USE exec() or eval() unless you know what you are doing and be aware of the risks.
https://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided


RE: equation - newbieAuggie2019 - Nov-07-2019

(Nov-07-2019, 09:25 AM)MathisDELAGE Wrote: I'd like to fix an equation with this program is what it would be possible to please

Hi!

I don't see the equation(s) you want to fix.

I would approach the issue in a different way, for instance:
import math

aValue = float(input("Please, for the equation type:\nax\u00b2 + bx + c = 0\nenter the value of coefficient 'a':\n"))
bValue = float(input("Please, for the equation type:\nax\u00b2 + bx + c = 0\nenter the value of coefficient 'b':\n"))
cValue = float(input("Please, for the equation type:\nax\u00b2 + bx + c = 0\nenter the value of coefficient 'c':\n"))
d = (bValue**2) - (4*aValue*cValue)
sol1 = (-bValue-math.sqrt(d))/(2*aValue)
sol2 = (-bValue+math.sqrt(d))/(2*aValue)
print(f'The solutions are x1 = {sol1} and x2 = {sol2}.')
and for the sample equation 'x² - x - 2 = 0', it would produce the following output:
Output:
Please, for the equation type: ax² + bx + c = 0 enter the value of coefficient 'a': 1 Please, for the equation type: ax² + bx + c = 0 enter the value of coefficient 'b': -1 Please, for the equation type: ax² + bx + c = 0 enter the value of coefficient 'c': -2 The solutions are x1 = -1.0 and x2 = 2.0. >>>
All the best,