4 回答
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([], [])
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零件,因为它会在不停止执行的情况下运行
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)
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
添加回答
举报