-2

I wrote this code to calculate the roots of a quadratic function when given the values for a, b, and c in the form ax^2+bx+c=0:

a = input("a")
b = input("b")
c = input("c")
print("Such that ", a, "x^2+", b, "x+", c, "=0,")
def greaterzero(a, b, c):
    x = (((b**2 - (4*a*c))**1/2) -b)/2*a
    return x

def smallerzero(a, b, c):
    x = (-1*((b**2 - (4*a*c))**1/2) -b)/2*a
    return x
if smallerzero(a, b, c) == greaterzero(a, b, c):
    print("There is only one zero for the quadratic given a, b, and c: ", 
greaterzero(a, b, c))
else:
    print ("The greater zero for the quadratic is ", greaterzero(a, b, c))
    print ("The smaller zero for the quadratic is ", smallerzero(a, b, c)) 

When I execute the program (in interactive mode) and input 1, 2, and 1 for a, b, and c, respectively, this is the output:

a1
b2
c1
Such that  1 x^2+ 2 x+ 1 =0,
Traceback (most recent call last):
  File "jdoodle.py", line 13, in <module>
    if smallerzero(a, b, c) == greaterzero(a, b, c):
  File "jdoodle.py", line 11, in smallerzero
    x = (-1*((b**2 - (4*a*c))**1/2) -b)/2
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

What's the issue here? I didn't formally learn how to use interactive mode yet. I'd like a simple explanation/introduction or a website/tutorial that provides one.

3 Answers3

2

You forgot to cast the input values to a numeric type.

a = int(input('a')) or a = float(input('a'))

Or, a bit cleaner:

def input_num(prompt):
    while True:
        try:
            return int(input(prompt + ': '))
        except ValueError:
            print('Please input a number')

a = input_num('a')
# ... etcetera
robinsax
  • 1,195
  • 5
  • 9
0

The problem here is that input takes the typed input as string type. Check if this makes it work:

a = int(input("Type the value of a: "))
b = int(input("Type the value of b: "))
c = int(input("Type the value of c: "))

Here you are explicitly changing the type of the input from str to integer so your variables can be handled by arithmetic operations.

Alejandro Lorefice
  • 797
  • 1
  • 5
  • 18
  • Thanks that worked! – user10059620 Sep 11 '18 at 23:35
  • But I read that raw_input interprets it as a string and input interprets it as an int... is that true? If so, how come you still need to specify that it's an int? – user10059620 Sep 11 '18 at 23:37
  • No that's not true, [`input()`](https://docs.python.org/3/library/functions.html#input) still returns a `string`: "The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that." https://docs.python.org/3/library/functions.html#input – chickity china chinese chicken Sep 11 '18 at 23:38
  • 1
    @user10059620: That is sort of true for Python 2, but it is not true for Python 3. All the answers here so far assume that you are running Python 3, which is the current standard version and is what any Python beginner should be learning. Which version of Python are you running? (Explaining why it is only sort-of true for Python 2 would take more time.) – Rory Daulton Sep 11 '18 at 23:39
  • Check [this](https://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x) to get a clear understanding on the differences. – Alejandro Lorefice Sep 11 '18 at 23:44
  • I'm running 3.6.5 @RoryDaulton – user10059620 Sep 12 '18 at 00:18
  • 1
    @user10059620: Then what you read is wrong for your situation, and you can use any of the three answers here so far. I upvoted the one by robinsax, but you should accept the one that helps you the most by clicking the checkmark at the upper-left of the answer. That is how you say thanks to the answerer, and also helps others to see that your question was answered. – Rory Daulton Sep 12 '18 at 00:21
0

You cannot do math with strings. As A.Lorefice said putting int in front of input will change the given string to an integer.

mark7328
  • 41
  • 5