Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
equation
#1
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
Reply
#2
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.
Reply
#3
Yes, I don't understand.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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?
Reply
#5
Nevertheless, DO NOT USE exec() or eval() unless you know what you are doing and be aware of the risks.
https://stackoverflow.com/questions/1933...be-avoided
Reply
#6
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020