7 Replies - 1075 Views - Last Post: 30 January 2015 - 01:57 AM Rate Topic: -----

#1 PlzHalpCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 15

Quadratic Equation Code help

Posted 29 January 2015 - 10:16 PM

I have been stuck on this for hours and hours on end please I know this is going to sound stupid. Sigh but ok

So I have to write a code that solves a quadratic equation. The user has to input in a,b,c for the quadratic equation. The rules for inputting are:

-The user must input three floating point numbers (do not worry if the user inputs
non floats and your program crashes). The user must not enter zero for A.
Prompt the user until the user provides valid input.

I'm having SO MUCH TROUBLE ON THIS PART!

Here is my code:
import math

def quad():

    temp = [] #initialize variables

    while (len(temp)!= 3):
        values = input("Input A B C>") #input string from line
        temp = values.split() #split string into list
    
##        if len(temp)==0:
##            values = input("Input A B C>")
##            
##        if len(temp)==1:
##            values = input("Input A B C>")
##           
##        if len(temp)==2:
##            values = input("Input A B C>")
##            
##        if len(temp)==3:
##            
        new_list=[]
        for item in temp:
            new_list.append(float(item))
      

    d = new_list[1]**2 - 4 * new_list[0] * new_list[2]
##    d = b**2 - 4*a*c

    if d < 0:
        x1 =(-new_list[1] + (d*-1)) / (2*new_list[0])
        x2 =(-new_list[1] + (d*-1)) / (2*new_list[0])

        if new_list[0] == 1:
            print("Solution for", "x^2", "+", new_list[1], "x", "+",new_list[2],"is:", str(x1)+"+i" , str(x2)+"-i")
        else:
            print ("Solution for", new_list[0],"x^2", "+", new_list[1], "x", "+",new_list[2],"is:", str(x1)+"+i" , str(x2)+"-i")
    
    elif d == 0:
        x = (-new_list[1]+math.sqrt(d))/(2*new_list[0])

        if new_list[0] == 1:
            print("Solution for", "x^2", "+", new_list[1], "x", "+",new_list[2],"is:", x)
        else:
            print ("Solution for", new_list[0],"x^2", "+", new_list[1], "x", "+",new_list[2],"is:", x)
    
    else:
        x1 = (-new_list[1]+math.sqrt(d))/(2*new_list[0])
        x2 = (-new_list[1]-math.sqrt(d))/(2*new_list[0])

        if new_list[0] == 1:
            print("Solution for", "x^2", "+", new_list[1], "x", "+",new_list[2],"is:", x1, "and", x2)
        else:
            print ("Solution for", new_list[0],"x^2", "+", new_list[1], "x", "+",new_list[2],"is:", x1, "and", x2)


 
quad()




THE WHILE LOOP IS WHERE I AM STRUGGLING! Ok so I want the program to know when the user is inputting only 0,1,or 2 numbers for a,b,c (since they have to input 3). I don't know how to do this! I'm struggling to write a while loop for this!

Is This A Good Question/Topic? 0
  • +

Replies To: Quadratic Equation Code help

#2 PlzHalpCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 15

Re: Quadratic Equation Code help

Posted 29 January 2015 - 10:26 PM

I just need someone to explain the set up of the while loop.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,989
  • Joined: 19-March 11

Re: Quadratic Equation Code help

Posted 29 January 2015 - 11:12 PM

Okay, let's look at the while loop. Here it is:


    while (len(temp)!= 3):
        values = input("Input A B C>") #input string from line
        temp = values.split() #split string into list
    
##        if len(temp)==0:
##            values = input("Input A B C>")
##            
##        if len(temp)==1:
##            values = input("Input A B C>")
##           
##        if len(temp)==2:
##            values = input("Input A B C>")
##            
##        if len(temp)==3:
##            
        new_list=[]
        for item in temp:
            new_list.append(float(item))
      




What's happening here? The loop will continue if, when you get to the top of the loop, the length of the temp list is not equal to three. Fine.
You ask the user for three values, separated by spaces, and you set temp to hold those values, split on whitespace. So far, so good.

Then, there's some commented code which I'll ignore, and then you make a new list, called new_list, and you populate it with each of the values the user gave you, coerced to floating-point values.

That all looks fine to me. What's the problem you're having?
Was This Post Helpful? 1
  • +
  • -

#4 PlzHalpCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 15

Re: Quadratic Equation Code help

Posted 29 January 2015 - 11:17 PM

the code I commented out are by accident. Like my program won't work; for example, when it says "Input ABC" I enter "12", instead of "123". The program is supposed to prompt you to input over and over again until you input three numbers (ex: 123). THe problem is, I cn enter a million numbers of I wanted to and it would keep telling me to input over and over again.

This post has been edited by andrewsw: 30 January 2015 - 12:31 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,989
  • Joined: 19-March 11

Re: Quadratic Equation Code help

Posted 29 January 2015 - 11:55 PM

Well, "123" is read as a single number. One hundred and twenty-three. Try entering numbers separated by spaces:

>>> temp = []
>>> while len(temp) != 3:
...   values = raw_input("enter numbers: ")
...   temp = values.split()
...   print "values = " + ','.join(temp)
... 
enter numbers: 123
values = 123
enter numbers: 12 345 567 678
values = 12,345,567,678
enter numbers: 123 456 789
values = 123,456,789
>>> temp
['123', '456','789']


Was This Post Helpful? 0
  • +
  • -

#6 PlzHalpCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 15

Re: Quadratic Equation Code help

Posted 30 January 2015 - 12:26 AM

WOW THANK YOU FOR TELLING ME TO INPUT IT WITH A SPACE! WOW IT ACTUALLY WORKED! THANK YOU!

This post has been edited by andrewsw: 30 January 2015 - 12:31 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,989
  • Joined: 19-March 11

Re: Quadratic Equation Code help

Posted 30 January 2015 - 12:41 AM

Glad I was able to help. Happy coding!
Was This Post Helpful? 0
  • +
  • -

#8 PlzHalpCode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 15

Re: Quadratic Equation Code help

Posted 30 January 2015 - 01:57 AM

View Postjon.kiparsky, on 30 January 2015 - 12:41 AM, said:

Glad I was able to help. Happy coding!


Sorry one more thing: how do I write calculating the code for complex roots when d<0?

if d < 0:
       
        x1 =(-new_list[1] + (d)) / (2*new_list[0]) #imaginary solutions
        x2 =(-new_list[1] - (d)) / (2*new_list[0])

        if new_list[0] == 1:
            print("Solution for", "x^2", "+", new_list[1], "x", "+",new_list[2],"is:", str(x1)+"+i" , str(x2)+"-i")
        else:
            print ("Solution for", new_list[0],"x^2", "+", new_list[1], "x", "+",new_list[2],"is:", str(x1)+"+i" , str(x2)+"-i")
    



the x1 and x2 doesn't work, it prints out roots but not the correct ones.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1