在 python 中,除了 elif、if 和 else 语句,我还能使用什么?
What else can I use instead of elif, if , else statements in python?
大家好我想知道除了 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.whosebug.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,我还能尝试什么?你能给我解释一下吗?我知道 elif 、 if 和 else statements.In 这种情况如果我想写其他命令我必须写;
elif command in "":
talkMe("")
do some """
elif command in "":
""""
等等..所以行太多了我可以让代码更短而不是elif语句吗?
还是我应该继续这样?
看来你可以用字典了
d = {'Hello Google': obj1, 'open my favourite web site': obj2}
你可以用字典。这是完整的例子
def switch_demo(argument):
switcher = {
"open web browser": "Opening your web browser",
"web browser": "Opening your web browser",
"open browser": "Opening your web browser",
"thank you": "You are welcome",
"thank you so much": "You are welcome",
"thanks": "You are welcome"
}
print(switcher.get(argument, "Invalid Command"))
command = "thank you"
switch_demo(command)
大家好我想知道除了 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.whosebug.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,我还能尝试什么?你能给我解释一下吗?我知道 elif 、 if 和 else statements.In 这种情况如果我想写其他命令我必须写;
elif command in "":
talkMe("")
do some """
elif command in "":
""""
等等..所以行太多了我可以让代码更短而不是elif语句吗? 还是我应该继续这样?
看来你可以用字典了
d = {'Hello Google': obj1, 'open my favourite web site': obj2}
你可以用字典。这是完整的例子
def switch_demo(argument):
switcher = {
"open web browser": "Opening your web browser",
"web browser": "Opening your web browser",
"open browser": "Opening your web browser",
"thank you": "You are welcome",
"thank you so much": "You are welcome",
"thanks": "You are welcome"
}
print(switcher.get(argument, "Invalid Command"))
command = "thank you"
switch_demo(command)