##### Cant multiply sequence by non-int type 'str'
 Cant multiply sequence by non-int type 'str' foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: Apr-24-2017, 02:04 PM 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     b2 = b*b TypeError: can't multiply sequence by non-int of type 'str'`````` Reply ichabod801 Bunny Rabbit Posts: 4,229 Threads: 97 Joined: Sep 2016 Reputation: Apr-24-2017, 02:16 PM (This post was last modified: Apr-24-2017, 02:17 PM by ichabod801.) 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. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures Reply foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: Apr-24-2017, 02:37 PM (This post was last modified: Apr-24-2017, 02:37 PM by foxtreat.) 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 x1a = float(-b)+float(sqd) TypeError: bad operand type for unary -: 'str'`````` Reply ichabod801 Bunny Rabbit Posts: 4,229 Threads: 97 Joined: Sep 2016 Reputation: Apr-24-2017, 02:45 PM 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. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures Reply foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: Apr-24-2017, 04:39 PM 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 ``` Reply ichabod801 Bunny Rabbit Posts: 4,229 Threads: 97 Joined: Sep 2016 Reputation: Apr-24-2017, 04:42 PM 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)`. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures Reply foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: Apr-24-2017, 05:28 PM 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!! Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Why can't I explicitly call __bool__() on sequence type? quazirfan 11 3,365 Aug-20-2021, 06:49 AM Last Post: Gribouillis Error : "can't multiply sequence by non-int of type 'float' " Ala 3 2,365 Apr-13-2021, 10:33 AM Last Post: deanhystad TypeError: can't multiply sequence by non-int of type 'float' DimosG 3 2,670 Apr-04-2020, 05:16 AM Last Post: michael1789 TypeError: can't multiply sequence by non-int of type 'str' emmapaw24 2 1,892 Feb-03-2020, 05:50 PM Last Post: michael1789 Type hinting - return type based on parameter micseydel 2 1,895 Jan-14-2020, 01:20 AM Last Post: micseydel TypeError: can't multiply sequence by non-int of type 'str' rregorr 2 1,938 Jun-28-2019, 04:43 PM Last Post: Yoriz TypeError: can't multiply sequence by non-int of type 'str' pythonnoob1234 4 3,576 May-21-2018, 06:12 PM Last Post: wavic

Forum Jump:

### User Panel Messages ##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020