如果在我的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)]()