[英]How to write switch case in python with dictionary
如果我的python代碼中的示例代碼為:
message = event_data["event"]
if message.get("subtype") is None :
if "friday" in message.get('text'):
callfridayfunction()
if "saturday" in message.get('text'):
callsaturdayfunction()
if "monday" in message.get('text'):
callmondayfunction()
如何將其寫為開關盒或使用字典? 請幫忙
這可能是最接近您想要的。 如果文本中也有多天,這將起作用。
days_switch = {
'monday': callmondayfunction,
'saturday': callsaturdayfunction
'friday': callfridayfunction
}
message = event_data["event"]
if message.get("subtype") is None :
text = message.get('text', '')
days = set(text.split()) & set(days_switch)
for day in days:
days_switch[day]()
如果您知道文本中沒有多天,則不需要循環。
days_switch[set(text.split()) & set(days_switch)]()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.