【发布时间】:2021-01-17 18:33:37
【问题描述】:
对不起,如果标题不正确,我是新手,正在自学 Python。我有一个包含这个的当前程序...
# Prompt function List
whatIsYourNameL = ['my name is','i am called', 'you may call me']
# Do this is if prompted
def whatIsYourName()
print(f'Hello {youName}')
while True:
if list(filter(lambda x: x in choice.lower(), changeYourNameList)):
changeYourName()
elif list(filter(lambda x: x in choice.lower(), whatIsYourNameList)):
whatIsYourName()
elif list(filter(lambda x: x in choice.lower(), whatIsMyNameList)):
whatIsMyName()
到目前为止,我在嵌套的 if - elif 语句中使用了相同的过滤器并更改了函数名称。我现在正试图通过尝试做这样的事情来简化......
# Prompt function List
whatIsYourNameL = ['my name is','i am called', 'you may call me']
# Do this is if prompted
def whatIsYourName()
print(f'Hello {youName}')
FunctList = ['changeYourName','whatIsYourName','whatIsMyName']
c = choice.lower
while True:
for funct in range(len(FunctList)):
if list(filter(lambda x: x in c, f'{FunctList[funct]}L')):
f'{FunctList[funct]}()'
关于我如何做到这一点的任何想法?
【问题讨论】:
-
您的代码中的
c是什么? -
你能在
FuncList定义中存储实际函数(即去掉引号)吗?这将使问题变得微不足道