Python Forum
TypeError: can't multiply sequence by non-int of type 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: can't multiply sequence by non-int of type 'str'
#1
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)
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
input returns a string use int or float on the returned string to do calculations.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,207 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,096 May-07-2022, 08:40 AM
Last Post: ibreeden
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,024 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  Why can't I explicitly call __bool__() on sequence type? quazirfan 11 4,539 Aug-20-2021, 06:49 AM
Last Post: Gribouillis
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 2,965 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,023 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,323 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  TypeError: 'type' object is not subscriptable Stef 1 4,440 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 2,571 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,149 May-17-2020, 05:56 PM
Last Post: shockwave

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020