使用关键字,这就是我为我的私人助理使用的。
我这样做的方式是创建了一个列表,里面有几个字符串,所以是这样的:
Hello_Keywords = ["Hello", "Cheers", "How are you"]
Jokes_Keywords = ["Tell me a joke", "Can you tell me a joke", "Make me laugh"
然后您要做的就是将这些列表分配给函数。所以是这样的:
def Hello():
print("Hello there")
def Jokes():
print("Why did the chicken cross the road?")
time.sleep(3)
print("To get to the other side!")
if User in Hello_KeyWords
Hello()
elif User in Jokes_Keywords
Jokes()
else:
print("Excuse me what?")
现在要回答您关于 Wikipedia 的具体问题,您可以按照以下方式进行操作:
User = "What is Python"
if "what is" in User:
User = User.split("is")
User = User[1].strip()
User = wikipedia.summary(User)
print(User)
上面的代码会检查用户是否说"what is",然后将变量拆分成一个列表["What", "Python"],然后取出第二部分(用户[1])并在维基百科上查找,然后,打印它。显然,您也可以使用其他关键字来执行此操作,例如 "who is" 或 "search Wikipedia for" 等
我就是这么用的,希望对你有帮助!