简体   繁体   English

如果 elif 和 else 以防万一,我该怎么办?

[英]How can I do if elif and else in case?

I am trying to make a spanish voice assistant.我正在尝试制作西班牙语语音助手。 I don't know how create a case with if , elif and else .我不知道如何用ifelifelse创建一个案例。

    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)

Looks like you have indentation error in your code.看起来您的代码中有缩进错误。 There's no switch in python so you have to use if-elif-else to do so. python 中没有开关,因此您必须使用 if-elif-else 来执行此操作。 If you want you can create a function which will work as switch case.如果你愿意,你可以创建一个 function 作为开关盒。 Like this:像这样:

def switch(texto):
    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)

or you can use dictionary mapping like this:或者您可以像这样使用字典映射:

def switch(texto):
    switcher = {"nada":"Ok estare para  ti cuando me necesites", "notenecesito":"Ok estare para  ti cuando me necesites", .......}
    return switcher.get(texto, None)

IndentationError: expected an indented block when try IndentationError:尝试时需要缩进块

def switch(texto):
    if texto == "nada":
        engine.say("Ok estare para  ti cuando me necesites")
        engine.runAndWait()

To simulate a switch statement in python, you can define a helper function like this:要在 python 中模拟 switch 语句,您可以像这样定义一个帮助器 function:

def switch(v): yield lambda *c: v in c

And use it in a C-like style:并以类似 C 的风格使用它:

for case in switch(texto):

    if case(nada, notenecesito):
       engine.say("Ok estare para ti cuando me necesites")
       engine.runAndWait()
       break
  
    if case(lahora):
       engine.say("La hora es"+ current_time)
       engine.runAndWait()
       print(Fore.RED+current_time)
       break
else:
    engine.say("buscando " + texto ,"Abriendo el navegador")
    engine.runAndWait()
    webbrowser.open("https://www.google.com/search?q="+texto)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能对 if/elif 进行理解,而不是其他? - How can I do a for comprehension with if/elif, and no else? 我如何获得if elif else语句重新启动 - How do I get an if elif else statement to restart if/elif/else 如何用 min() 和 max() 替换? - How can if/elif/else be replaced with min() and max()? 切换/案例使用情况,如何有效使用if-elif-else功能? - Switch/Case usage, how to efficiently use if-elif-else functionality? 我还能用什么来代替 python 中的 elif、if、else 语句? - What else can I use instead of elif, if , else statements in python? 如何重命名内置函数,例如“if”->“hehe”、“elif”->“haha”、“else”->“hihi”? - How can I rename builtin functions e.g "if" -> "hehe", "elif" -> "haha", "else" -> "hihi"? 我怎样才能让我的代码正常工作? 用户输入不适用于 if、elif 和 else - How can i get my code to work correctly? user input is not working with if, elif, and else 如何缩短 Python 中的 if、elif、elif 语句 - How can I shortern if, elif, elif statements in Python 如果elif else阻止评估每个案例 - if elif else blocks evaluate for every case GUIZERO可以通过传感器开发吗? 如果可能,怎么做? (使用 If Elif 和 Else 语句?) - Can GUIZERO develop by sensor? If possible, how? (Using If Elif and Else statement?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM