我想做一个西班牙语语音助手。我不知道如何用if
、elif
和else
创建一个案例
if texto == nada:
engine.say("Ok estare para ti cuando me necesites")
engine.runAndWait()
elif texto == notenecesito:
engine.say("Ok estare para ti cuando me necesites")
engine.runAndWait()
elif texto == lahora:
engine.say("La hora es"+ current_time)
engine.runAndWait()
print(Fore.RED+current_time)
else:
engine.say("buscando " + texto ,"Abriendo el navegador")
engine.runAndWait()
webbrowser.open("https://www.google.com/search?q="+texto)
看起来代码中有缩进错误。python中没有开关,因此必须使用if-elif-else。如果需要,您可以创建一个函数,该函数将用作开关盒。像这样:
或者您可以使用字典映射,如下所示:
缩进错误:尝试时应为缩进块
要在python中模拟switch语句,可以定义如下所示的助手函数:
并以类似C的方式使用它:
相关问题 更多 >
编程相关推荐