将列表传递给导入的函数

时间:2018-05-17 18:39:58

标签: python python-module

我将以下程序中的函数放入模块中,但是当我将模块导入单独的程序并调用函数时,我得到一个错误(见下文)。如何使用模块正确运行函数需要做什么?任何帮助深表感谢。

原创计划:

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行,in     pf.print_models(unprinted_designs,completed_models)   在print_models中的文件" / home / pi / Documents / Python Projects / python_work / printing_functions.py",第11行     completed_models.append(current_design) NameError:name' completed_models'未定义

0 个答案:

没有答案