![]() |
|
[Python] Help making quadratic formula in python - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Help making quadratic formula in python (/showthread.php?tid=5660505) |
Help making quadratic formula in python - Gerymc - Jul 2, 2017 I am trying to make a program in which you enter the A, B, and C variables in the quadratic formula and it find's X's two values. I already set up an if-else statement so that if the discriminant is negative, it will tell the user there are no solutions, however I am having trouble on the actual math part. The quadratic formula is x = -b±√b^2-4ac all divided by 2a. Part of my code is below. As you can see in the else statement, I set up two variables because X can have two different answers because there is the plus-minus sign. Would the way I did it be the correct way to do it? When I try to test it out, it gives the wrong answers so my math is wrong somehow. Code #Makes a variable for the discriminantEDIT: Realized that I need to make the formula like this: Code (-b+((b**2 - 4*a*c)**.5))/2*a* - slober - Jul 2, 2017 You can write it a bit shorter if you wanted to. ex. Code import cmath* - Gerymc - Jul 2, 2017 (Jul 2, 2017 01:37 PM)slober Wrote: You can write it a bit shorter if you wanted to. I'm doing this for a class and they didn't teach us that yet, so I can't use it. * - slober - Jul 2, 2017 (Jul 2, 2017 01:41 PM)jdugerymc Wrote: I'm doing this for a class and they didn't teach us that yet, so I can't use it. This would be my full solution Code import cmathor Code print ('x1 = {0} and x2 {1}'.format(solution1,solution2))* - Gerymc - Jul 2, 2017 (Jul 2, 2017 01:54 PM)slober Wrote: This would be my full solution It is just a summer class "Computational Thinking Part 1" I believe. It is a prerequisite for other computer science classes. But I already fixed it, thanks for the help though. I forgot to add some parentheses. * - arkoydas - Jul 3, 2017 Is there any way to do it fast with list comprehension? |