TypeError: unsupported operand type in Python

Scuba

'Homework' Question in Python Book: Write a program that prompts the user to enter 3 points of a triangle angle and display its area, using the equations given:

s = (side1 + side2 + side3) / 2
area = √s(s - side1)(s - side2)(s - side3)

The program I wrote when executed, displays an error, which I don't understand the root cause of and how to fix.

   #Calculate the area of a triangle
   # Enter coordinates for 3 points
   sideOne = 1.5, -3.4#(x1, y1) = eval(input("Enter coords for side1:"))
   sideTwo = 4.6, 5#(x2, y2) = #eval(input("Enter coords for side2:"))
    sideThree = 9.5, -3.4#(x3, y3) = #eval(input("Enter coords for side3:"))

   # Calculate s value
    sideAll = (sideOne + sideTwo + sideThree) / 2# Compute Area
    area = (sideAll(sideAll - sideOne)(sideAll - sideTwo)(sideAll - sideThree)) * * 0.5# Display Area
    print("The Area of entered Triangle:", area)

Resulting Error

Traceback(most recent call last):
  File "J:\Programming\PROGRAMMING\Python\Exercises\Chapter 2\Programming Exercises - 2.14 Skip.py", line 8, in < module >
  sideAll = (sideOne + sideTwo + sideThree) / 2
TypeError: unsupported operand type(s) for / : 'tuple'
and 'int'

d6bels

When you do sideOne = 1.5, -3.4, then sideOne is a tuple.

So it is basically (as you say in your comments) fthe coordinates of one of your 3 points in the form of (x,y), here (1.5, -3.4).
You cannot do arithmetic operations directly on tuples, it has no sense to do this because tuples are just a way of representing data and python does not guess that it is a coordinate.
Instead you can do mathematical operations on numerical values (integer, float and so on) but not on tuples.

Instead, you might want to compute the length of your side and then use it as given in your formula.

You might want to try something like this :

import math

#Calculate the area of a triangle
# Enter coordinates for 3 points
a = 1.5, -3.4#(x1, y1) = eval(input("Enter coords for side1:"))
b = 4.6, 5#(x2, y2) = #eval(input("Enter coords for side2:"))
c = 9.5, -3.4#(x3, y3) = #eval(input("Enter coords for side3:"))

# Calculate the length of the sides from the 3 triangle points
sideOne = math.sqrt( (b[0]-a[0])**2 + (b[1]-a[1])**2 )
sideTwo = math.sqrt( (c[0]-b[0])**2 + (c[1]-b[1])**2 )
sideThree = math.sqrt( (a[0]-c[0])**2 + (a[1]-c[1])**2 )

# Calculate s value
sideAll = (sideOne + sideTwo + sideThree) / 2 # Compute Area
area = math.sqrt(sideAll * (sideAll - sideOne) * (sideAll - sideTwo) * (sideAll - sideThree)) # Display Area
print("The Area of entered Triangle:", area)

Note I prefer to use the math.sqrt function rather than non-integer power but this is just a matter of taste.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unsupported operand type in Python

From Dev

Python 2.7 TypeError: Unsupported operand type(s) for %:'None type' and str

From Dev

Python Error TypeError: unsupported operand type(s) for +: 'function' and 'function'

From Dev

Python (TypeError: unsupported operand type(s) for Add: 'undefined' and 'str')

From Dev

Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

From Dev

Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

From Dev

TypeError: unsupported operand type(s) for -: 'str' and 'int' Python

From Dev

Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

From Dev

Python (TypeError: unsupported operand type(s) for Add: 'undefined' and 'str')

From Dev

TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType' in Python 3

From Dev

Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

From Dev

Python27 TypeError: unsupported operand for type(s) += 'int' and 'str'

From Dev

Python Type Error Unsupported Operand

From Dev

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

From Dev

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

From Dev

TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for |: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for |: 'list' and 'list'

From Dev

TypeError: unsupported operand type(s) for -: 'list' and 'list'

From Dev

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'Element'

From Dev

TypeError: unsupported operand type(s) for //: 'int' and 'graph'

From Dev

TypeError: unsupported operand type(s) for -: 'module' and 'LinearSegmentedColormap'

From Dev

TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

From Dev

Pandas to_sql TypeError unsupported operand type

Related Related

  1. 1

    Unsupported operand type in Python

  2. 2

    Python 2.7 TypeError: Unsupported operand type(s) for %:'None type' and str

  3. 3

    Python Error TypeError: unsupported operand type(s) for +: 'function' and 'function'

  4. 4

    Python (TypeError: unsupported operand type(s) for Add: 'undefined' and 'str')

  5. 5

    Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

  6. 6

    Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

  7. 7

    TypeError: unsupported operand type(s) for -: 'str' and 'int' Python

  8. 8

    Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

  9. 9

    Python (TypeError: unsupported operand type(s) for Add: 'undefined' and 'str')

  10. 10

    TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType' in Python 3

  11. 11

    Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

  12. 12

    Python27 TypeError: unsupported operand for type(s) += 'int' and 'str'

  13. 13

    Python Type Error Unsupported Operand

  14. 14

    TypeError: unsupported operand type(s) for +=: 'int' and 'list'

  15. 15

    TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

  16. 16

    TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

  17. 17

    TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

  18. 18

    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

  19. 19

    TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

  20. 20

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

  21. 21

    TypeError: unsupported operand type(s) for |: 'int' and 'str'

  22. 22

    TypeError: unsupported operand type(s) for |: 'list' and 'list'

  23. 23

    TypeError: unsupported operand type(s) for -: 'list' and 'list'

  24. 24

    TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

  25. 25

    TypeError: unsupported operand type(s) for +: 'int' and 'Element'

  26. 26

    TypeError: unsupported operand type(s) for //: 'int' and 'graph'

  27. 27

    TypeError: unsupported operand type(s) for -: 'module' and 'LinearSegmentedColormap'

  28. 28

    TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

  29. 29

    Pandas to_sql TypeError unsupported operand type

HotTag

Archive