Import 如何在python中使用一个文件的输出作为另一个文件的输入
就像我有密码一样Import 如何在python中使用一个文件的输出作为另一个文件的输入,import,python-import,Import,Python Import,就像我有密码一样 def poly (x,a): j=0 ans=[0]*len(x) while (j<len(x)): i=0 while(i<len(a)): ans[j] = ans[j] + a[i]*((x[j])**i) i=i+1 else: j=j+1 else: print ans a=[1,1,1
def poly (x,a):
j=0
ans=[0]*len(x)
while (j<len(x)):
i=0
while(i<len(a)):
ans[j] = ans[j] + a[i]*((x[j])**i)
i=i+1
else:
j=j+1
else:
print ans
a=[1,1,1]
x=[1,1,1]
print poly(x,a)
也许这是一个可能的解决办法 首先:在函数定义中,有“print ans”和“print x”,我认为您需要返回语句。i、 e.“返回ans”和“返回x”。 e、 g 对于我写上述内容的方式,需要注意的是,poly.py和mult.py文件必须与导入它们的程序位于同一目录/文件夹中。(一个简单的替代方法是简单地定义函数(粘贴它们)来代替导入语句) 此外,如果您只想像我的示例中那样将它们作为导入函数使用,则可以删除除实际函数def之外的所有代码。否则,当执行polymult.py时,您将看到在导入poly.py和mult.py期间执行的额外屏幕打印
def mult(x,q):
i=0
while(i<len(x)):
x[i] = x[i]*q
i=i+1
else:
print x
x=[1,1,1]
q=2
print mult(x,q)
def poly (x,a):
j=0
ans=[0]*len(x)
while (j<len(x)):
i=0
while(i<len(a)):
ans[j] = ans[j] + a[i]*((x[j])**i)
i=i+1
else:
j=j+1
else:
return ans # changed print to return
#start code
# import programs poly.py and mult.py
# this will execute commands in these files
# but gives access to functions defined within
from poly import *
from mult import *
# Main
# define/reset the variables we need/want to use
a=[1,1,1]
x=[1,1,1]
q=2
# pass mult which returns new x into poly
print poly(mult(x,q),a)
#end code