访问在函数内部定义的列表

时间:2019-10-03 18:55:21

标签: python python-3.x

运行代码时,它会按计划要求输入,但是未定义列表alpha。

首先,输入内容未读取,因此我更改了缩进方式,但是没有解决定义问题。

def newclassification1(alpha,bravo):
        alpha = ["Jacob", "Jane", "Jim"]
        bravo = ["Male", "Female", "Unknown"]

    name = input("What is the persons name?")
    if name in alpha:
        while True:
            print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

错误消息:

  

回溯(最近一次通话最后一次):文件“”,第7行,在    NameError:名称“ alpha”未定义

5 个答案:

答案 0 :(得分:0)

我做了一些调整,主要是从函数中删除alpha和bravo。

alpha = ["Jacob", "Jane", "Jim"]
bravo = ["Male", "Female", "Unknown"]
def newclassification1(alpha,bravo):
    name = input("What is the persons name?")
    if name in alpha:
        print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")
newclassification1(alpha,bravo)

之所以可行,是因为由于您要将alpha和bravo传递给函数,因此这些变量应在函数外部定义。另外,我将您的while更改为if,因为否则,您将获得无限的输出。 我要共享的功能的输出:

What is the persons name?Jim
Unknown
What is the persons name?Jane
Female
What is the persons name?a
The persons name is not in the register.

给出OP注释:alphabravo 必须 必须在功能中定义。

def newclassification1(alpha = [],bravo = []):
    alpha = ["Jacob", "Jane", "Jim"]
    bravo = ["Male", "Female", "Unknown"]
    name = input("What is the persons name?")
    if name in alpha:
        print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")
newclassification1()

输出:

What is the persons name?Jim
Unknown
What is the persons name?Tim
The persons name is not in the register.
What is the persons name?Jane
Female

答案 1 :(得分:0)

我不知道您的最终目的,但是这两个代码在 Python3 上没有任何错误。

第一个:

def newclassification1(alpha,bravo):
    name = input("What is the persons name?")
    if name in alpha:
        while True:
            print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

newclassification1(["Jacob", "Jane", "Jim"], ["Male", "Female", "Unknown"])

第二个:

def newclassification1(alpha,bravo):
    alpha = ["Jacob", "Jane", "Jim"]
    bravo = ["Male", "Female", "Unknown"]
    name = input("What is the persons name?")
    if name in alpha:
        while True:
            print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

newclassification1([], [])

答案 2 :(得分:0)

def newclassification1(name):
    alpha = ["Jacob", "Jane", "Jim"]
    bravo = ["Male", "Female", "Unknown"]
    if name in alpha:
        print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

name = input("What is the persons name?")

newclassification1(name)

答案 3 :(得分:0)

正如其他人指出的那样,您应该修复缩进,并找出带有您的论点的东西。 首先,当您在函数中定义add_action( 'admin_enqueue_scripts', create_function( '', "wp_enqueue_style( 'pb_backupbuddy-wp-admin', '" . pb_backupbuddy::plugin_url() . "/css/wp-admin.css', array(), pb_backupbuddy::settings( 'version' ) );" ) alpha时,每次调用它们都将被重写,我想您不想发生这种情况。 其次,您应该修复缩进。我建议您具备以下条件:

bravo

通过这种方式,默认情况下,您将使用def newclassification1(alpha=["Jacob", "Jane", "Jim"],bravo=["Male", "Female", "Unknown"]): name = input("What is the persons name?") if name in alpha: print(bravo[alpha.index(name)]) else: print("The persons name is not in the register.") alpha=["Jacob", "Jane", "Jim"],但是稍后调用该函数时,可以轻松地将其更改为所需的内容。

此外,您不需要bravo=["Male", "Female", "Unknown"]部分,因为它将在不停止执行的情况下运行

答案 4 :(得分:0)

扩展Celius答案:

alpha = ["Jacob", "Jane", "Jim"]
bravo = ["Male", "Female", "Unknown"]
def newclassification1(alpha,bravo):
    name = input("What is the persons name?")
    print(bravo[alpha.index(name)] if (name in alpha) else "xxxxx")
newclassification1(alpha,bravo)

只需使用Python三元运算并将结果直接发送到print语句