访问函数内部定义的列表

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


慕桂英4014372
浏览 114回答 4
4回答

慕婉清6462132

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

qq_笑_17

正如其他人所指出的,您应该修复缩进,并用您的论点找出一些东西。首先,当你在函数中定义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"]但稍后在调用该函数时可以轻松地将其更改为您想要的任何内容。此外,您不需要while True零件,因为它会在不停止执行的情况下运行

慕妹3242003

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)

芜湖不芜

我做了一些调整,主要是从函数中删除 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?JimUnknownWhat is the persons name?JaneFemaleWhat is the persons name?aThe persons name is not in the register.给定 OP 注释:alpha并且bravo 必须在函数中定义。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?JimUnknownWhat is the persons name?TimThe persons name is not in the register.What is the persons name?JaneFemale
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python