Bottom Page

• 1 Vote(s) - 1 Average
• 1
• 2
• 3
• 4
• 5
 Cant multiply sequence by non-int type 'str' foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: 0 Likes received: 0 #1 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") + dhere 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' ichabod801 Bunny Rabbit Posts: 3,837 Threads: 83 Joined: Sep 2016 Reputation: 231 Likes received: 1163 #2 Apr-24-2017, 02:16 PM (This post was last modified: Apr-24-2017, 02:17 PM by ichabod801. Edited 1 time in total. Edit Reason: There were six of us but now we are five. ) 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 foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: 0 Likes received: 0 #3 Apr-24-2017, 02:37 PM (This post was last modified: Apr-24-2017, 02:37 PM by foxtreat. Edited 1 time in total.) 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' ichabod801 Bunny Rabbit Posts: 3,837 Threads: 83 Joined: Sep 2016 Reputation: 231 Likes received: 1163 #4 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 foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: 0 Likes received: 0 #5 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 ichabod801 Bunny Rabbit Posts: 3,837 Threads: 83 Joined: Sep 2016 Reputation: 231 Likes received: 1163 #6 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 foxtreat Programmer named Tim Posts: 14 Threads: 7 Joined: Apr 2017 Reputation: 0 Likes received: 0 #7 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!! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post TypeError: can't multiply sequence by non-int of type 'str' rregorr 2 288 Jun-28-2019, 04:43 PM Last Post: Yoriz TypeError: can't multiply sequence by non-int of type 'str' pythonnoob1234 4 1,195 May-21-2018, 06:12 PM Last Post: wavic

Forum Jump:

Users browsing this thread: 1 Guest(s)