3

I am getting an error which tells me I can't multiply two variables of a specific value.

TypeError: can't multiply sequence by non-int of type 'str'

I am trying to make a Pythagorean theorem inside of python for school. I need to have it inside of a float to get a decimal number.

I have already tried a couple of different things,

  • I've put it inside of a couple values, int, string, float etc.
  • I just tried a lot of different things, this is the best I got this far.
    l_1 = float(input())
    l_1 = float(l_1)
    l_1 = str(l_1)
    print ("The long side is: " + l_1)
    l_2 = float(input())
    l_2 = float(l_2)
    l_2 = str(l_2)
    print ("The short side is: " + l_2)
    l_2 = int(l_2)
    l_1 = int(l_1)
       
    l_1 = int(l_1)
    l_2 = int(l_2)
        
    wor1 = math.sqrt(l_1 * l_1 - l_2 * l_2)
    print (wor1)

I expect the output to actual be the answer without any error codes, I just need it to calculate with the variables it is given.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    you are trying to perform a ```sqrt``` and ```multiplication``` on strings, you need to convert them to either ```float()``` or ```int()``` depending what type of numbers you have, try this ```wor1 = math.sqrt(float(l_1) * float(l_1) - float(l_2) * float(l_2))``` – Mr.Riply Oct 04 '19 at 11:28

2 Answers2

2

A couple of changes to the code and you are good to go.

Please mind that, While calculating the square root, be careful of passing the absolute difference of the squares in the sqrt function. Using this, you can remove the convention of small and large side. Just take two sides and code will handle this for you.

import math

l_1 = float(input())
print ("The long side is: " + str(l_1))
l_2 = float(input())
print ("The short side is: " + str(l_2))

difference = float(l_1 * l_1 - l_2 * l_2)
# Take absolute difference since square roots of negative numbers are undefined
absolute_difference = math.fabs(difference)

# Get square root of the absolute difference 
wor1 = math.sqrt(absolute_difference)
print (wor1)
Anshuman Tiwari
  • 424
  • 9
  • 19
1

just print the float values iself, no need to convert them to a string in the first place

l_1 = float(input())
print ("The long side is: ", l_1)
l_2 = float(input())
print ("The short side is: ", l_2)

wor1 = math.sqrt(l_1 * l_1 - l_2 * l_2)
print (wor1)
Quastiat
  • 1,164
  • 1
  • 18
  • 37