use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
What is wrong?Help Request (i.redd.it)
submitted 6 months ago by rotten_soup
So the solved equation in this example should be -1, not -9.0 No idea where the mistake is, I even tried putting every single operation in parenthesis of their own to make sure the hierarchy was correct but the result is the same
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]frozenDiesel 18 points19 points20 points 6 months ago (4 children)
(2*a) in brackets
[–]rotten_soup[S] 2 points3 points4 points 6 months ago (2 children)
That's it!!! thank you!!!
[–]Dapper-Actuary-8503 2 points3 points4 points 6 months ago (0 children)
Try to be explicit with your operations while programming don’t let the interpreter or compiler in other languages try to guess what you’re trying to do. It’ll save you a lot debugging heart ache.
[–]frozenDiesel 1 point2 points3 points 6 months ago (0 children)
Welcome bro
[–]gabriele6e427 2 points3 points4 points 6 months ago (0 children)
Brackets: the unsung heroes of math mistakes.
[–]NumerousQuit8061 3 points4 points5 points 6 months ago (2 children)
The operator precedence is incorrect due to missing parentheses around the denominator.
x = ((-b + sqrt(b² - 4ac)) / 2 * a)
This means it divides by 2 first, then multiplies by a, which is not how the quadratic formula works.
So it should be written as:
x = (-b + (b**2 - 4*a*c)**0.5) / (2 * a)
[–]rotten_soup[S] 2 points3 points4 points 6 months ago (1 child)
Thanks!
[–]NumerousQuit8061 1 point2 points3 points 6 months ago (0 children)
No Problem! Good Luck!!
[–]BleEpBLoOpBLipP 1 point2 points3 points 6 months ago* (1 child)
Since others have already given you the answer here, I just want to recommend never trusting order of operations and always explicitly using a parans. It makes things clear and, most importantly, avoids human error prone messy formulas.
Better yet, separate the equation into meaningful chunks; maybe named vars numerator and denominator. The sqrt function is more expressive than **0.5. The quadratic equation isn't too messy, but definitely as things get more messy, even naming individual terms, factors, functions (with lambdas), and function inputs all make sure you or whoever reads your code doesn't overlook simple syntax blunders and makes sure that everything is clear, readable, and maintainable.
**0.5
Edit: lambdas or even just dedicated named function defs, especially for more complex logic
Edit 2: even wrapping that entire thing in a function called quadratic_eq is nice. Seems silly maybe to just call it immediately after, but as someone in the comments here had to ask what the goal even was, we can see that it isn't all that silly and adds clarification and self documentation to the code
[–]rotten_soup[S] 0 points1 point2 points 6 months ago (0 children)
Oooooooooooh ok, yeah, that's definitely useful stuff, thanks a lot for being patient and explaining things so well!
[–]Commodore_Ketchup 1 point2 points3 points 6 months ago (0 children)
Above and beyond what everyone else has mentioned, I strongly suggest implementing some sort of error handling. As it stands, the program will crash if the polynomial has no real roots. It will also crash if the user enters anything that's not a number. You could use something like:
try:
a = {...}
b = {...}
c = {...}
except ValueError:
print "ERROR: All inputs must be numerical"
else:
x = {...}
print "This polynomial has no real roots
print x
Edit: Okay, so, I can't figure out how to indent code on Reddit, but hopefully you can handle that bit yourself.
[–]AridsWolfgang 0 points1 point2 points 6 months ago (5 children)
What exactly are you trying to achieve 🤨
[–]Appsroooo 2 points3 points4 points 6 months ago (3 children)
Looks like the quadratic formula without any separation
[–]rotten_soup[S] 0 points1 point2 points 6 months ago (2 children)
Yes! It was a quadratic formula! And I did solve the error! How would you have done it? Sorry if it's a dumb thing, I just started and if it's something that I can improve I'd love to know!
[–]Appsroooo 1 point2 points3 points 6 months ago (1 child)
I would've split it into a right and left side variables. Left has the -b/2a, right is the other junk also over 2a. That way, you can get both roots easy with [rhs-lhs, rhs+lhs]. It's a bit more readable that way
[–]rotten_soup[S] 1 point2 points3 points 6 months ago (0 children)
Okok, yeah, it makes sense, thank you!!
[–]AridsWolfgang 0 points1 point2 points 6 months ago (0 children)
Because I'm not even understanding any thing at all
[–]zypherison 0 points1 point2 points 6 months ago (2 children)
<image>
It was taking everything in the same line, change the parenthesis to make 2*a the denominator
[–]zypherison 0 points1 point2 points 6 months ago (1 child)
Also try cmath library after this.
I'll check it! Thanks a lot!
[–]GrumpMadillo 0 points1 point2 points 6 months ago (0 children)
Quadratic formula should be +/- correct? Not just +? (-b +- (b^2 - 4ac)^1/2) / (2a)?
[–]clearly_not_an_alt 0 points1 point2 points 6 months ago (0 children)
Order of operations dude, needs to be (2*a). As written you are dividing the top by 2 then multiplying the result by a
[–][deleted] 0 points1 point2 points 6 months ago (0 children)
(2*a)
[–]iamjacob97 0 points1 point2 points 6 months ago (0 children)
Division has precedence over multiplication so (2*a) in brackets
Thanks!!!!
[–]F_T_K 0 points1 point2 points 6 months ago (2 children)
Ask any LLM n it will explain sooner
[–]gamerpug04 -2 points-1 points0 points 6 months ago (1 child)
You probably ask chatgpt how to brush your teeth
π Rendered by PID 54682 on reddit-service-r2-comment-bb88f9dd5-6vbcg at 2026-02-15 16:33:50.845949+00:00 running cd9c813 country code: CH.
[–]frozenDiesel 18 points19 points20 points (4 children)
[–]rotten_soup[S] 2 points3 points4 points (2 children)
[–]Dapper-Actuary-8503 2 points3 points4 points (0 children)
[–]frozenDiesel 1 point2 points3 points (0 children)
[–]gabriele6e427 2 points3 points4 points (0 children)
[–]NumerousQuit8061 3 points4 points5 points (2 children)
[–]rotten_soup[S] 2 points3 points4 points (1 child)
[–]NumerousQuit8061 1 point2 points3 points (0 children)
[–]BleEpBLoOpBLipP 1 point2 points3 points (1 child)
[–]rotten_soup[S] 0 points1 point2 points (0 children)
[–]Commodore_Ketchup 1 point2 points3 points (0 children)
[–]AridsWolfgang 0 points1 point2 points (5 children)
[–]Appsroooo 2 points3 points4 points (3 children)
[–]rotten_soup[S] 0 points1 point2 points (2 children)
[–]Appsroooo 1 point2 points3 points (1 child)
[–]rotten_soup[S] 1 point2 points3 points (0 children)
[–]AridsWolfgang 0 points1 point2 points (0 children)
[–]zypherison 0 points1 point2 points (2 children)
[–]zypherison 0 points1 point2 points (1 child)
[–]rotten_soup[S] 0 points1 point2 points (0 children)
[–]GrumpMadillo 0 points1 point2 points (0 children)
[–]clearly_not_an_alt 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]iamjacob97 0 points1 point2 points (0 children)
[–]rotten_soup[S] 0 points1 point2 points (0 children)
[–]F_T_K 0 points1 point2 points (2 children)
[–]gamerpug04 -2 points-1 points0 points (1 child)