I'm a beginner in python, and have worked on making a script that solves quadratic functions. The code works, as far as I'm aware, and I've even implemented some error detection. I'm looking for some critique on the code, anything that'll help me make the code better/cleaner, and anything that will help me in my knowledge of python.
Ok. So post the code? lol
My bad, trying to figure out the forum, do I just paste in the code as text?
Inside [python ][/ python] tags, yes.
print("spam")If you hit quote on my post, you can see what the tags look like.
If it is relatively short script or code snippet you can post here on the forum in python tags. Don't forget to remove formatting (from IDE, web IDE) if present. for more info see
BBcode help If it is longer project it would be better to set-up online repository (e.g. on Github, Bitbucket, etc.) and post link to it.
Don't post screenshots or upload zip files.
import math print("Welcome to John Freeland's quadratic formula calculator!") while True: CanCalc=True print("Enter the corresponding values of the standard quadratic function: y=ax^2+bx+c") a=input("Enter the A value:") b=input("Enter the B value:") c=input("Enter the C value:") try: a=float(a) b=float(b) c=float(c) except ValueError: CanCalc=False if((a!=0)&(CanCalc==True)): try: SqRoot=math.sqrt((b*b)-(4*a*c)) Plus=(-b+SqRoot)/(2*a) Minus=(-b-SqRoot)/(2*a) print("\nx-intercept 1:", Plus,"\n" "x-intercept 2:", Minus,"\n") except ValueError: print("\n") print("The numbers you inserted don't work for some reason, maybe your function has no x-intercepts.") print("\n") else: if(a==0): print("\nThe \"A\" coefficient can't be equal to zero.\n") if((a==0)&(CanCalc==False)): print("and") if(CanCalc==False): print("\nYou can't enter letters.\n") Quote: if(a==0): print("\nThe \"A\" coefficient can't be equal to zero.\n") if((a==0)&(CanCalc==False)): print("and") if(CanCalc==False): print("\nYou can't enter letters.\n")
Why not...
if(a==0): print("\nThe \"A\" coefficient can't be equal to zero.\n") if(CanCalc==False): print("and") print("\nYou can't enter letters.\n")You've got a while loop, but no way to end the loop. How do you stop the program? ctrl+c?
You do a lot of bitwise comparisons... in python, the ideomatic way to do that is simply using the
and keyword (so,
if a==0 and not CanCalc: for example)
Yea, that part of my code was iffy, I had a hard time finding out how to combine the errors, so thanks for pointing out the shortcut. Also, if you wouldn't mind, how would I go about including a method of stopping the loop? I was looking into some way of detecting a keyword entered into one of the variable inputs, but this seems like a weird way of going about it.
(May-02-2017, 04:57 PM)zeevo234 Wrote: [ -> ]....... CanCalc=True
- It is customary to surround operators with single space
- Pythonic variable names are snake-style -
can_calc
(May-02-2017, 04:57 PM)zeevo234 Wrote: [ -> ] print("Enter the corresponding values of the standard quadratic function: y=ax^2+bx+c") a=input("Enter the A value:") ........
Use lsit instead of individual variables - it makes you code shorter and scalable, e.g. - if you wanted to make cubic instead of quadratic equation, just enlarge you list
(May-02-2017, 04:57 PM)zeevo234 Wrote: [ -> ] if((a!=0)&(CanCalc==True)):
- Never compare to boolean directly
if CanCalc does the job
- Brackets are not necessary - matter of taste, but why write redundant symbols?
& is bitwise and - it will work, but stick to boolean counterpart, less chances to get in trouble
There's more stuff, but I will take a break, In the meantime, please read
PEP-8 That link is really useful, thanks. I'll spend some time to figure out the more proper ways of formatting things. The lsit thing sounds interesting, I'll take a look at that as well.
(May-02-2017, 05:13 PM)nilamo Wrote: [ -> ]Quote: if(a==0): print("\nThe \"A\" coefficient can't be equal to zero.\n") if((a==0)&(CanCalc==False)): print("and") if(CanCalc==False): print("\nYou can't enter letters.\n")
Why not...
if(a==0): print("\nThe \"A\" coefficient can't be equal to zero.\n") if(CanCalc==False): print("and") print("\nYou can't enter letters.\n")
I have the if statement with the variable CanCalc separate from the other error detection, so that they can display separately as well as simultaneously.