Python Forum

Full Version: TypeError: can't multiply sequence by non-int of type 'str'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using the follow code to calculate the hypotenuse and legs of a rectangle triangle. If any one of the 3 given values(a, b ou c) is "x", it would be to calculate other variables. But, when I run the code, I get the message bellow:
Traceback (most recent call last):
File "main.py", line 7, in <module>
quadradosA_B = int(b*b)+int(c*c)
TypeError: can't multiply sequence by non-int of type 'str'


import math
a = input('Informe a hipotenusa: ')
b = input('Informe o Cateto A: ')
c = input('Informe o Cateto B: ')

if (a == 'x'):
  quadradosA_B = int(b*b)+int(c*c)
  hipotenusa = math.sqrt(quadradosA_B)
  print('A hipotenusa é: ', hipotenusa)

elif (b == 'x'):
  quadradoCatetoB = int(a*a)-int(c*c)
  CatetoB = math.sqrt(quadradoCatetoB)
  print('Cateto B vale: ', CatetoB)

elif (c == 'x'):
  quadradoCatetoC = int(a*a)-int(b*b)
  CatetoC = math.sqrt(quadradoCatetoC)
  print('Cateto C vale: ', CatetoC)
The input function returns a string. You need to convert that value to a number before doing math with it. So rather than int(b * b) you want something like int(b) * int(b).
input returns a string use int or float on the returned string to do calculations.