Alright, this is how my newest program looks like:

import math

def main():
    print('This program finds the real solutions to a quadratic')
    print

    s = float(input('Please enter the coefficients (a,b,c): '))
    a, b, c = s.split(',')

    
    discRoot = float(math.sqrt(b * b - 4 * a * c))
    root1 = (-b + discRoot) / (2*a)
    root2 = (-b - discRoot) / (2*a)

    print
    print('The solutions are:', root1, ('and'), root2)

main()

and i get this error msg:
in main
in main
s = float(input('Please enter the coefficients (a,b,c): '))
ValueError: invalid literal for float(): 2, 3, -2


Help ._.

Recommended Answers

All 4 Replies

I assume you are using Python3?

When you are splitting the input string you still get strings, so each coefficients (a,b,c) has to be converted to a float before you apply it to your formula.

Correct, I use python3.


I deleted the float infront of the input since it was causing the program to bug. But shouldnt I be able to tell python to make all variables that i put in the input to be floats? if not,
how do I make them float nicely?

Correct, I use python3.


I deleted the float infront of the input since it was causing the program to bug. But shouldnt I be able to tell python to make all variables that i put in the input to be floats? if not,
how do I make them float nicely?

Basically you have to iterate over your "split" coefficients and then perform float conversion on each one independently. When you split them they return a list, which you'll be doing your iteration on. Here's the steps broken down:

>>> my_coeffs = input('Enter coefficients (a,b,c): ')
Enter coefficients (a,b,c): 3, 5.5, 4.25
>>> coeff_list = my_coeffs.split(',')
>>> coeff_list
['3', ' 5.5', ' 4.25']
>>> # Create an empty list to store our float values
>>> float_list = []
>>> # Iterate over our string coefficients
>>> for each in coeff_list:
...     # turn each one into a float
...     conv_coeff = float(each)
...     # and finally store it
...     float_list.append(conv_coeff)
...     
>>> float_list
[3.0, 5.5, 4.25]
>>>

But thankfully in python you can make it look a little more beautiful than the above using list comprehension:

>>> my_coeffs = raw_input('Enter coefficients (a,b,c): ')
Enter coefficients (a,b,c): 2, 4.5, 5.25
>>> float_list = [float(each) for each in my_coeffs.split(',')]
>>> float_list
[2.0, 4.5, 5.25]
>>>

List comprehension allows us to create a list from another iterable while performing some action (and there's even room for logic). This is a powerful tool that you would probably benefit from learning.

Here's the documentation about it

Just modify your code, also take care of negative numbers in your square root ...

import math

def main():
    print('This program finds the real solutions to a quadratic')
    print()

    s = input('Please enter the coefficients (a,b,c): ')
    a, b, c = s.split(',')
    a = float(a)
    b = float(b)
    c = float(c)

    disc = b * b - 4 * a * c
    if disc < 0:
        print("root will give imaginary number")
        return
    discRoot = math.sqrt(disc)
    root1 = (-b + discRoot) / (2*a)
    root2 = (-b - discRoot) / (2*a)

    print()
    print('The solutions are:', root1, ('and'), root2)

main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.