为了账号安全,请及时绑定邮箱和手机立即绑定

访问函数内部定义的列表

访问函数内部定义的列表

慕桂英4014372 2022-06-14 10:59:07
运行代码时,它会按计划要求输入,但未定义列表 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' 未定义
查看完整描述

4 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

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


查看完整回答
反对 回复 2022-06-14
?
qq_笑_17

TA贡献1818条经验 获得超7个赞

正如其他人所指出的,您应该修复缩进,并用您的论点找出一些东西。首先,当你在函数中定义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零件,因为它会在不停止执行的情况下运行


查看完整回答
反对 回复 2022-06-14
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

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)


查看完整回答
反对 回复 2022-06-14
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

我做了一些调整,主要是从函数中删除 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 注释: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?Jim

Unknown

What is the persons name?Tim

The persons name is not in the register.

What is the persons name?Jane

Female


查看完整回答
反对 回复 2022-06-14
  • 4 回答
  • 0 关注
  • 113 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信