Python Forum
Programs can be improved
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programs can be improved
#1
Hi, my name is fabien.
I'm in highschool.
I started python language yesterday.
I want to know if the programs can be improved.
I try to do my best.

Ax²+bx+c=0, solved eq 2nd
#Solved Eq 2nd Ax²+bx+c=0
print("Saissisez a,b,c")
a = input("a=")
a = int(a)
b = input("b=")
b = int(b)
c = input("c=")
c = int(c)
d = b*b-4*a*c                                          #d=delta
print("Delta=",d)
from math import*
if d>0 :
        print("deux solutions: x1 et x2") 
        x1 = -b-(sqrt(d))                               #Seul moyen trouvé x1 =-b-(racinecarree(d)) formule en 1 ne fonctionne pas
        x1 = x1/(2*a)                                   #x1 = -b-(racinecarree(d))/(2*a)
        print("x1=",x1)
        x2 = -b+(sqrt(d))
        x2 = x2/(2*a)
        print("x2=",x2)
if d==0 :
        print("une solution: x0")
        x0 = -b/(2*a)
        print("x0=",x0)
else :
        print("aucune  solution")
abs |x|

x = input("X=")
x = int(x)
if x>=0 :
        print("|X|=",x)
else :
        print("|x|=",-x)
Thank you :p.
Have a nice day.
Reply
#2
The second can be replaced entirely by the builtin abs(): 
x = int(input("X="))
print("|X|={0}".format(abs(x)))
Also, one-character variable names are not a good habit to get in.  Use variables that mean something, "x" doesn't mean anything.
Try to avoid "from ___ import *".  Import * is not a good thing, and I wish it either wasn't allowed, or was a syntax error... especially since you only use it for no reason other than to get sqrt().

But it does work.  So congrats on getting that far on your own :)
Reply
#3
Thank you. I learn a lot. :)
Reply


Forum Jump:

User Panel Messages

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