假设我们有一个列表my_list=["a","b","c"].我想要做的是创建空列表
a=[]
b=[]
c=[]
Run Code Online (Sandbox Code Playgroud)
这样我就可以根据他们的名字将一些元素附加到它们中.
我在同一个".py"文件中编写两个单独的函数时遇到了一个奇怪的情况.我使用的是python 2.7.12.这是我的代码:
def do_the_job(n, list):
for i in range(2, n):
a = list[i - 1]
b = list[i - 2]
ap_me = list[i- a] + list[i - b]
list.append(ap_me)
return list
def func_two(n, k):
counter = 0
list = [1, 1]
do_the_job(n,list)
for i in range(n):
if list[i] >= k:
counter += 1
return counter
Run Code Online (Sandbox Code Playgroud)
然后导入,我写入from my_py import func_two控制台.当我写func_two(10,3)它导入do_the_job函数也成功运行.但是,这里有帖子的原因,当我写入do_the_job(7,[1,1])控制台时,它出错了NameError: name 'do_the_job' is not defined.
为什么我可以使用do_the_job函数作为func_two导入函数的一部分func_two …