我在运行此代码时遇到问题,因为它一直在执行 Python 中 if 语句的随机部分



所以我正在开发一个助手类型的程序,但我计划对它进行更多的自定义,以获得比您通常的助手更具体的用法,我的代码遇到了问题,有时它会在应该执行的 if 语句旁边执行随机 if 语句,有时它只会抛出一个错误,我不知道为什么会发生这种情况, 至少现在还没有..任何帮助将不胜感激。

编辑:我更新了代码以包含 elif (代码更新 #2(

def check_audio():
cmdtext = get_audio()
print("CMDTEXT-You said: " + cmdtext)
try:
if any(x.lower() in cmdtext for x in ["Hey", "Hello", "Ahoi"]):
hellos = ["Hey there!", "Hi!", "How's it going?", "Well.. Hello there!"]
stalk(random.choice(hellos))
elif "schedule" in cmdtext:
if "days" or "day" in cmdtext:
number = re.search(r'd+', cmdtext).group()
if w2n.word_to_num(cmdtext) == int:
get_events(w2n.word_to_num(cmdtext), service)
elif "Week" in cmdtext:
get_events(7, service)
if w2n.word_to_num(cmdtext) & "Week" in cmdtext:
combined = w2n.word_to_num(cmdtext) * 7
get_events(number, service)
elif "quit" in cmdtext:
quitcount + 1
print(quitcount)
byebyes = ["Bye Bye!", "See you later!", "Cya later alligator", "See you soon!"]
stalk(random.choice(byebyes))
quit()
elif "go back" in cmdtext:
#This part is not currently functional and does absolutely nothing
while "go back" in cmdtext:
break
elif "open" in cmdtext:
if "Notepad" in cmdtext:
subprocess.Popen([r'C:Program FilesNotepad++\Notepad++.exe'])
elif "Chrome" in cmdtext:
subprocess.Popen([r'C:Program Files (x86)GoogleChromeApplication\Chrome.exe'])
elif "Discord" in cmdtext:
subprocess.Popen([r'C:UsersCelestialMavisAppDataLocalDiscordapp-0.0.306\Discord.exe'])
elif "Steam" in cmdtext:
subprocess.Popen([r'C:Program Files (x86)Steam\Steam.exe'])
else:
stalk("I did not recognize that program name")
if any(y.lower() in cmdtext for y in ["Rick Roll", "rickroll"]):
webbrowser.open_new_tab("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
else:
print("I don't recognise this command")
except Exception as e:
print("Error" + e)
stalk("I didn't quite get that")

编辑#1 有没有办法让程序回到代码首次运行的上一个.py文件并在某个点开始,我在第一个文件中运行一个侦听命令的侦听器,一旦它得到命令,它就会运行这个.py文件来识别命令,问题是一旦完成,它就会继续侦听命令,而不是返回侦听关键字这是我使用的代码:

def mainOps():
while beginop != "Quit" or "Exit":
SEGI.check_audio()
# Listen for Keyword from Microphone!
recog = spcrec.Recognizer()
# This is the keyword that will be used to activate the system in this case its "hey Wendy"
cmdwrd = 'hey Wendy'
# This is the keyword that will cause the program to quit
cmdwrdqt = 'quit'
# This while loop listens for the keyword and then executes mainOps() if it's detected
with spcrec.Microphone() as source:
#with spcrec.AudioFile('keyword.wav') as source:
print(f"Hey! Im VASE")
print(f"Please say {cmdwrd.capitalize()} to speak with Wendy or say {cmdwrdqt.capitalize()} to End the program")
# The variable below dictates what VASE TTS says upon startup
Greeting = f"Hey! I am VASE"
whattosay = f"Please say {cmdwrd} to speak with Wendy or say {cmdwrdqt} to End the program"
# Sending the variable to the TTS function in order to create an MP3 file of the text and play it
vtts.VaseTTS(whattosay) #Strangely this only works once and I have no idea why.
# Attempted to call the function again using different text but it doesn't function yet
vso = "Vase Online"
print(vso)
#STa.talk(vso)
# Code below continues to listen to what you say and does something when it hears the keyword
while True:
try:
STa.detectionwords = STa.get_audio()
if (STa.check_audio() == True):
while True:
mainOps()
else:
print("Please speak again, keyword not detected")
except Exception as e:
print('No keywords detected, please say the command to begin again')
print(e)

这个(以及类似的行(

if "Hey".lower() or "Hello".lower() or "Ahoi".lower() in cmdtext:

应该是

if any(x.lower() in cmdtext for x in ["Hey", "Hello", "Ahoi"]):

in不会在多个or上"分布",类似于数学中的乘法分布在加法上。给定orin的优先级,您的原件等效于

if "Hey".lower() or "Hello".lower() or ("Ahoi".lower() in cmdtext):

# Legal, but long-winded
if "Hey".lower() in cmdtext or "Hello".lower() in cmdtext or "Ahoi".lower() in cmdtext:

由于"Hey".lower()是一个非空字符串,因此永远不会计算其他两个表达式,并且整个事情True"Hey".lower()因为 是真实的。

if "Hey".lower() or "Hello".lower() or "Ahoi".lower() in cmdtext:

像上面这样的很多声明可能与您的要求不同,也许是这样表达的

if "Hey".lower() in cmdtext or "Hello".lower() in cmdtext or "Ahoi".lower() in cmdtext:

或者也许你这样做

from itertools import repeat
cmdtext = 'John, good morning'
cmdtext = cmdtext.lower()
print(any(map(str.__contains__, repeat(cmdtext), ['hey', 'hello', 'ahoi'])))

类似于上面的答案,试试这个。

if cmdtext in ("Hey".lower(),"Hello".lower(),"Ahoi".lower()):

注释掉 while 循环,看看它是否有效,因为它目前不可用。

最新更新