Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表传递给导入的函数_Python_Python Module - Fatal编程技术网

Python 将列表传递给导入的函数

Python 将列表传递给导入的函数,python,python-module,Python,Python Module,我将以下程序中的函数放入一个模块中,但当我将该模块导入一个单独的程序并调用这些函数时,我得到一个错误(见下文)。我需要做什么才能使用模块使函数正确运行?非常感谢您的帮助 原始程序: def print_m(unprinted, completed): while unprinted: current = unprinted.pop() print("Printing: " + current) completed.append(curren

我将以下程序中的函数放入一个模块中,但当我将该模块导入一个单独的程序并调用这些函数时,我得到一个错误(见下文)。我需要做什么才能使用模块使函数正确运行?非常感谢您的帮助

原始程序:

def print_m(unprinted, completed):
    while unprinted:
        current = unprinted.pop()
        print("Printing: " + current)
        completed.append(current)

def show_completed(completed):
    print("\nThe following have been printed:")
    for completed_item in completed:
        print(completed_item)

unprinted = ['star', 'sword', 'model']
completed = []

print_m(unprinted, completed)
show_completed(completed)
将函数放入模块-printing_functions.py:

def print_m(unprinted, completed):
    while unprinted:
        current = unprinted.pop()
        print("Printing: " + current)
        completed.append(current)

def show_completed(completed):
    print("\nThe following have been printed:")
    for completed_item in completed:
        print(completed_item)
编写了单独的程序来导入模块:

import printing_functions as pf

unprinted = ['star', 'sword', 'model']
completed = []

pf.print_m(unprinted, completed)
pf.show_completed(completed)
运行该程序时,出现以下错误:

NameError: name 'completed' is not defined

更新:

我在上面的代码中使用了较短的术语,它看起来确实有效,这让我更加困惑。在下面发布原始代码。如果上面的工作,为什么下面的模块不工作

def print_models(unprinted_designs, completed_designs):
    """
    Simulate printing each design, until none are left.
    Move each design to completed_models after printing.
    """
    while unprinted_designs:
        current_design = unprinted_designs.pop()

         #Simulate creating a 3D print from the design.
        print("Printing model: " + current_design)
        completed_models.append(current_design)

def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []

print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

模块

def print_models(unprinted_designs, completed_designs):
    """
    Simulate printing each design, until none are left.
    Move each design to completed_models after printing.
    """
    while unprinted_designs:
        current_design = unprinted_designs.pop()

         #Simulate creating a 3D print from the design.
        print("Printing model: " + current_design)
        completed_models.append(current_design)

def show_completed_models(completed_models):
    """Show all the models that were printed."""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

新节目

import printing_functions as pf

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []

pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)

回溯:

回溯(最近一次呼叫最后一次): 文件“/home/pi/Documents/Python Projects/Python_work/8-15_printing_models.py”,第6行,在 pf.打印模型(未打印的设计、已完成的模型) 文件“/home/pi/Documents/Python Projects/Python\u work/printing\u functions.py”,第11行,在print\u模型中 已完成的\u模型。追加(当前\u设计)
NameError:name“completed_models”没有如上所述定义

,这可以正常工作;我无法重现这个错误。请你发布整个回溯,而不仅仅是最后一行吗?@Fortunato谢谢你的回复。我最初发布的代码确实有效,这让我更加困惑。我缩短了条件,使其更容易张贴,以某种方式解决了问题。我继续发布了完整的程序,上面有完整的回溯。我已经一遍又一遍地看了一遍,但是当我将函数拆分成一个单独的模块时,仍然会产生一个错误。@Josh我编辑了我的文章,以包含完整的回溯和我正在处理的完整代码。当我在我最初的帖子中缩写这些术语时,它不知怎么解决了我的问题,但我还是不知道怎么做。谢谢你的帮助,找到了!术语“完成的设计”被输入到打印模型的定义中,而不是“完成的模型”。不知何故,当所有内容都在一个文件中时,它仍然运行良好,但当我将函数复制到一个模块文件中时,它挂起了。