trying to make a script that solves a simple math problem (example x(2)+4x-2=0)
however i get this error and can't find why i get it,i went from python 2 to 3 and had to add () to the print commands ,even if i change input to raw_input and try it in python 2 i get the same error
here is my code
import math
print ("Ax*2+Bx+C=0")
print ("what is your A?")
a = input('my a is: ')
print ("what is your B?")
b = input('my b is: ')
print ("what is your C?")
c = input('my c is: ')
print ("calculating")
b2 = b*b
d = b2-4*a*c
sqd = math.sqrt(d)
x1a = -b+sqd
x2a = -b-sqd
ax2 = a*2
x1 = x1a/ax2
x2 = x2a/ax2
print ("your X1 is ") + x1 + ("your X2 is") + x2
print ("your discriminant is") + d
here is the error
Error:
Traceback (most recent call last):
File "math.py", line 10, in <module>
b2 = b*b
TypeError: can't multiply sequence by non-int of type 'str'
If you are using Python 3.x, input returns a string. You need to convert to integer (or float, if that is what you are expecting).
>>> int('801') == 801
True
>>> 4 * int('27')
108
The float() function does the same for numbers with a decimal part.
i need it to be able to handle numbers like 2.31 and -12.9 for example so i set it as float if im correct
current code is this,it still fails
import math
print ("Ax*2+Bx+C=0")
print ("what is your A?")
(a) = input('my a is: ')
print ("what is your B?")
(b) = input('my b is: ')
print ("what is your C?")
(c) = input('my c is: ')
print ("calculating")
float(a)
float(b)
float(c)
b2 = float(b)*float(b)
float(b2)
d = float(b2)-4*float(a)*float(c)
float(d)
sqd = math.sqrt(d)
x1a = float(-b)+float(sqd)
float(x1a)
x2a = float(-b)-float(sqd)
float(x2a)
ax2 = float(a)*2
float(ax2)
x1 = float(x1a)/float(ax2)
float(x1)
x2 = float(x2a)/float(ax2)
float(x2)
print ("your X1 is " + x1 + "your X2 is") + x2
print ("your discriminant is" + d )
error here
Error:
Traceback (most recent call last):
File "math.py", line 18, in <module>
x1a = float(-b)+float(sqd)
TypeError: bad operand type for unary -: 'str'
The float function doesn't change the variable, it creates a new floating point number from the string. So you need to assign it to something. Often you just do a = float(a)
. Then you don't have to keep converting after the initial conversion. That should clear up your error.
thank you!after some tinkering i managed to get the code to run AND made it work better
here is the final deal capable of solving a certain math problem
import math
print ("Ax*2+Bx+C=0")
print ("what is your A?")
(a) = input('my a is: ')
print ("what is your B?")
(b) = input('my b is: ')
print ("what is your C?")
(c) = input('my c is: ')
print ("calculating")
a = float(a)
b = float(b)
c = float(c)
b2 = b*b
b2 = float(b2)
d = b2-4*a*c
d = float(d)
if d < 0:
d = str(d)
print ("\n" * 200)
print (" D is Less than 0,There are no answers")
d = str(d)
exit
d = float(d)
if d >= 0:
d = str(d)
print ("D is: " + d )
d = float(d)
sqd = math.sqrt(d)
sqd = float(sqd)
x1a = -b+sqd
x1a = float(x1a)
x2a = -b-sqd
x2a = float(x2a)
ax2 = a*2
ax2 = float(ax2)
x1 = x1a/ax2
x1 = str(x1)
x2 = x2a/ax2
x2 = str(x2)
if d >= 0:
d = str(d)
print ("\n" * 200)
print ("your X1 is " + x1 + " your X2 is " + x2)
print ("your discriminant is " + d )
exit
Again, you don't need to keep floating things. b2 is the product of two floats, so it's already a float, so you don't need b2 = float(b2)
.
oh sorry,i didn't know if float+float=float or str and decided to play safe,anyway the code works and that extra kilobyte won't do much harm
MANY thanks!!