繁体   English   中英

访问 Function 内部定义的列表

[英]Accessing Lists Defined inside Function

运行代码时,它会按计划要求输入,但未定义列表 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.")

错误信息:

Traceback(最近一次调用最后一次):文件“”,第 7 行,在 NameError:名称 'alpha' 未定义

我做了一些调整,主要是从 function 中删除 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 传递给 function,因此这些变量应该在 function 之外定义。 此外,我将您的while更改为if ,否则您将获得无限的输出。 Output 为 function 我正在分享:

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()

Output:

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

我不知道您的最终目的,但这两个代码在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([], [])
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)

正如其他人所指出的,您应该修复缩进,并用您的 arguments 找出一些东西。 首先,当你在 function 中定义alphabravo时,每次调用它们都会被重写,我想你不希望发生这种情况。 其次,你应该修复缩进。 我建议有以下几点:

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"]但以后可以轻松将其更改为您想要的任何内容当您致电 function 时。

此外,您不需要while True部分,因为它会在不停止执行的情况下运行

扩展 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 三元运算并将结果直接发送到打印语句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM