TypeError:Python中不支持的操作数类型

时间:2015-02-25 10:12:44

标签: python typeerror

Python书中的“家庭作业”问题: 编写一个程序,提示用户输入三角形的三个点,并使用给出的公式显示其面积:

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

我执行时编写的程序显示错误,我不明白其根本原因和解决方法。

   #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)

结果错误

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'

2 个答案:

答案 0 :(得分:0)

(side1 + side2 + side3)结果是元组。

e.g。

>>> sideOne = 1.5, -3.4
>>> sideTwo = 4.6, 5
>>> sideThree = 9.5, -3.4
>>> type(sideOne)
<type 'tuple'>
>>> sideOne + sideTwo + sideThree
(1.5, -3.4, 4.6, 5, 9.5, -3.4)
>>> a = sideOne + sideTwo + sideThree
>>> type(a)
<type 'tuple'>
>>> 

不能将元组除以整数,它会引发TypeError异常。

e.g。

>>> (1,2)/2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

使用Pythagoras theorem获取每一方的长度。

代码:

import math

# Three co-ordinates. 
sideOne = 1.5, -3.4
sideTwo = 4.6, 5
sideThree = 9.5, -3.4

# Find lenght of each side by Pythagoras theorem.
side1_x, side1_y = abs(sideOne[0] - sideTwo[0]), abs(sideOne[1] - sideTwo[1])
side2_x, side2_y = abs(sideTwo[0] - sideThree[0]), abs(sideTwo[1] - sideThree[1])
side3_x, side3_y = abs(sideThree[0] - sideOne[0]), abs(sideThree[1] - sideOne[1])

len_12 =  math.sqrt(side1_x*side1_x + side1_y*side1_y)
len_23 =  math.sqrt(side2_x*side2_x + side2_y*side2_y)
len_31 =  math.sqrt(side3_x*side3_x + side3_y*side3_y)
print "Length of sides:- ", len_12, len_23, len_31

输出:

Length of sides:-  8.95377015564 9.72471079261 8.0

答案 1 :(得分:0)

执行sideOne = 1.5, -3.4后,sideOnetuple

所以基本上(正如你在评论中所说的那样)(x,y)形式的{3}点之一的坐标,(1.5, -3.4)
你不能直接对元组进行算术运算,因为元组只是表示数据的一种方式而python不会猜测它是一个坐标,所以没有必要这样做。 相反,你可以对数值(整数,浮点数等)进行数学运算,但不能对元组进行数学运算。

相反,您可能想要计算您身边的长度,然后按照公式中的说明使用它。

你可能想尝试这样的事情:

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)

注意我更喜欢使用math.sqrt函数而不是非整数幂,但这只是一个品味问题。