我正在尝试做一些命令切换。比If Ifse更智能
if 'Who' in line.split()[:3]:
Who(line)
elif 'Where' in line.split()[:3]:
Where(line)
elif 'What' in line.split()[:3]:
What(line)
elif 'When' in line.split()[:3]:
When(line)
elif 'How' in line.split()[:3]:
How(line)
elif "Make" in line.split()[:3]:
Make(line)
elif "Can You" in line.split()[:3]:
CY(line)
else:
print("OK")
所以说明。如果Who
,What
等位于命令的前3个字中,则执行相应的功能。我只想知道是否有一个更明智的方法来做到这一点,而不是很多if
,elif
和else
?
'case'或'switch',无论哪个在python中支持 –
使用'dictionary'。 –
@TheBrofessor:呃,都不支持。 – DSM