我被困在使用多参数通过函数。我有两份文件:
1.
import function
x=2
y=5
print function.power(x,y)function.py
import math
def power(*x)
return math.pow(x,x)每当我试图将多个参数传递给power函数时,它都会出现以下错误:
跟踪(最近一次调用):
文件“C:\示例\1.py”,第33行,在 打印function.power(x,y) 文件"c:\Examples\function.py",第11行 返回math.pow(x,x) TypeError:需要一个浮点数
发布于 2013-01-31 05:17:29
我想你想:
def power(*x):
return math.pow(*x)这是一种理解力的形式。在power函数中,x是一个tuple,当传递到另一个函数时,它可以被解压缩。
发布于 2013-01-31 06:27:58
这是因为*x实际上是让x成为一个列表。
你真的想让function.py成为:
import math
def power(*x)
return math.pow(x[0],x[1])为什么你真的想知道怎么做呢?显然,不可能毫无意义地包装math.pow函数。
https://stackoverflow.com/questions/14618942
复制相似问题