Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 30th, 2007, 10:36 PM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Quadratic Formula in Python

Quote:
And print out the output in a way the user can understand.
Your code needs print statements if you want to see output. Print out all four variables, root1r, root1i, root2r, root2i, in a format you want your user to see.

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 )
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 10:42 PM   #12
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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()
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 10:44 PM   #13
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 10:45 PM   #14
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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.
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 10:49 PM   #15
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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()
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 10:49 PM   #16
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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)
    print

But 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
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 10:57 PM   #17
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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.
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 11:00 PM   #18
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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? =/
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 11:02 PM   #19
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
Re: Quadratic Formula in Python

OMG!! IT WORKED!!! The code worked!!! Yaaaaaaaaaaaaaaaaaaaaaay!!!! Thank you Sane!!!
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 11:04 PM   #20
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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!
acwbrat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC