![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Quadratic Formula in Python
Quote:
A useful thing would be to use "format specifiers". I don't want to confuse you, but this code might help, if you feel ambitious enough to understand it: Essentially, each %s is replaced with the next variable enclosed inside the brackets. name = "Aaron" language = "Python" word = "fun" print "Hello, my name is: %s. I program in %s and have a lot of %s with it."%( name, language, word ) |
|
|
|
|
|
|
#12 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
What am I doing wrong?
import math
def main(): # beginning of the code
print "Hello! This program finds the real and complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
disc = (b*b-4*a*c) # discriminant
if disc < 0:
disc = -disc # force it to be positive
else:
print "This quadratic does not have imaginary roots"
return
discRoot = math.sqrt(b*b-4*a*c)
root1r = (-b) / (2 * a) # the real portion of root1
root1i = discRoot / (2 * a) # the imaginary portion of root1
root2r = root1r # the real portions are the same for each root
root2i = -root1i # the only difference is it's negative
main() |
|
|
|
|
|
#13 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Quadratic Formula in Python
You used to have
discRoot = math.sqrt(disc) But for some reason you've now got it as discRoot = math.sqrt(b*b-4*a*c) Put it back. Also, you're still not printing your output at the end. |
|
|
|
|
|
#14 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
When inputting a=1 b=-10 c=34
The code doesn't display the results but rather goes back to the prompt. |
|
|
|
|
|
#15 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
Here is what I have right now:
import math
def main(): # beginning of the code
print "Hello! This program finds the real and complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
disc = (b*b-4*a*c) # discriminant
if disc < 0:
disc = -disc # force it to be positive
else:
print "This quadratic does not have imaginary roots"
return
discRoot = math.sqrt(disc)
root1r = (-b) / (2 * a) # the real portion of root1
root1i = discRoot / (2 * a) # the imaginary portion of root1
root2r = root1r # the real portions are the same for each root
root2i = -root1i # the only difference is it's negative
print %s %s %s %s root1r, rooti, root2r, rooti
print
main() |
|
|
|
|
|
#16 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Quadratic Formula in Python
Your print syntax doesn't match my example. You need quotations. You need the % sign. And you need () around your variables. Syntax in programming can be understandably picky. If you follow my previous example.
print "%s %s %s %s"%(root1r, rooti, root2r, rooti)
printBut you got some of your variable names wrong. rooti should be root1i, same with root2i. Then you can format it more nicely like so: print "%s+%si , %s+%si"%(root1r, root1i, root2r, root2i)
print |
|
|
|
|
|
#17 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
I'm sorry but what happened to the imaginary numbers or the complex numbers for the quadratic formula? I had to refresh myself on calculating the quadratic formula and I don't know if this code it computing the imaginary numbers/complex numbers. Maybe I am just confused.
|
|
|
|
|
|
#18 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Quadratic Formula in Python
This code is outputting the real and imaginary parts of the complex roots.
For example. y = 1x^2 + 2x + 2 has the roots "-1+1i and -1-1i". Running the program with 1, 2, 2 verifies that. Let me guess, all along you wanted the complex roots in polar form? =/ |
|
|
|
|
|
#19 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
OMG!! IT WORKED!!! The code worked!!! Yaaaaaaaaaaaaaaaaaaaaaay!!!! Thank you Sane!!!
|
|
|
|
|
|
#20 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Re: Quadratic Formula in Python
All along, I've been doing this code wrong by using import cmath and also just not knowing the tricks and trades in %s %i. That's wonderful!!! I feel a lot better now!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| If you want to learn Python or improve your Python skills | coldDeath | Python | 2 | Nov 23rd, 2005 12:27 AM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 8:55 AM |
| Advanced Python Tricks | Arevos | Python | 19 | Sep 24th, 2005 7:39 AM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 12:41 PM |