大家好,想知道我们还能用什么来代替elif、if、else语句?或者如何将给定的if、elif、else语句更改为任何其他方法
假设ı有这样的语音助手
webb = ["open web browser","web browser", "open browser"]
thkns = ["thank you","thank you so much", "thanks"]
fav_web = ["open my favourite web site","favourite web site","my best web site"]
hwaru = ["how are you", "what's up", "how is going"]
thtime = ["whats the time" , "the time", "time"]
def assistant(command):
if command in webb:
talkMe("Opening your web browser")
webbrowser.open("https://www.google.com.tr")
elif command in thkns:
talkMe("You are welcome")
elif command in fav_web:
talkMe("Opening your site")
webbrowser.open("www.stackoverflow.com")
elif command in hwaru:
msg = ["ı am good, you?", "good", "not bad"]
talkMe(random.choice(msg))
elif command in thtime:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
talkMe(f"The time is {strTime} ")
所以我想知道,除了埃利夫,我还能尝试什么?你能给我解释一下吗?ı知道elif、if和else语句。在这种情况下,如果我想编写其他命令,ı必须编写
elif command in "":
talkMe("")
do some """
elif command in "":
""""
等等。。因此,行太多会使代码比elif语句更短吗? 还是我应该继续这样
你可以用字典。这是一个完整的例子
看来你可以用字典了
相关问题 更多 >
编程相关推荐