2014-01-21 126 views
-1
a = 3,3 b = 5,3 
a2 = a**2 
b2 = b**2 
eq1_sum = a2 + 2ab + b2 
eq2_sum = a2 - 2ab + b2 
eq1_pow = (a + b)**2 
eq2_pow = (a - b)**2 
print ’First equation: %g = %g’, % (eq1_sum, eq1_pow) 
print ’Second equation: %h = %h’, % (eq2_pow, eq2_pow) 

这个程序就显示错误:不支持的操作类型错误

TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'

回答

2

你需要的,如果你想用一个浮点数使用.

a = 3.3 
b = 5.3 
a2 = a**2 
b2 = b**2 

否则,使用逗号,创建一个元组:

>>> a = 3,3 
>>> type(a) 
<type 'tuple'> 
>>> a = 3.3 
>>> type(a) 
<type 'float'> 

你现在得到一个错误,因为ab是元组,你不能提高元组的权力。

相关问题