【发布时间】:2020-05-09 09:53:55
【问题描述】:
我有以下功能
def retrieve_phone_data():
# function does something
return phone_list
def retrieve_customer_data():
# function does something
return customer_info
def main(input1, input2):
if input1 == 'phone' and input2 == 'customer':
retrieve_phone_data()
retrieve_customer_data()
else:
print('Please enter valid input')
input1 和 input2 只能有特定的值(让我们说 "phone" 和 "customer" 现在)。我有 10 多个函数,我不想使用 If 语句,因为它太多了。
我需要像 excel 中的 vlookup 这样的东西,我将创建可能的输入列表和共同赞助函数,以便每个有效的用户输入都将链接到该列表中的一个函数。有什么办法吗?
【问题讨论】:
-
您的问题的要点很清楚,但是您的代码示例除了缩进不正确(我提交了编辑)之外,可能也不是您想要的。您可能希望在
input1 == 'phone'时调用retrieve_phone_data并在input2 ==customer'` 时调用retrieve_customer_data,而不是仅在两个相等条件成立时调用这两个函数。
标签: python python-3.x list user-defined-functions