Elif 语句错误并一次又一次地打开 desktop.ini。为什么?
Elif statement Misbehaving and open desktop.ini again and again. Why?
这段代码有问题,我不知道为什么。它一次又一次地执行相同的 elif 语句,没有任何输入和 return desktop.ini 文件,请告诉我如何解决这个问题以及为什么会这样。如果我只放置 5 或 6 个 elif 语句,那么它可以正常工作,但在第 6 个语句之后它就不起作用或开始行为不端。
desktop.ini 文件代码为
[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21790
InfoTip=@%SystemRoot%\system32\shell32.dll,-12689
IconResource=%SystemRoot%\system32\imageres.dll,-108
IconFile=%SystemRoot%\system32\shell32.dll
IconIndex=-237
import pyttsx3
import wikipedia
import speech_recognition as sr
import os
import webbrowser
import datetime
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if 0 <= hour < 12:
speak("Good Morning!")
elif 12 <= hour < 18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("I am your assistant Sir. Please tell me how may I help you")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
# print(e)
print("Say that again please...")
return "None"
return query
def wikipedia(query):
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
def youtube():
webbrowser.open("youtube.com")
def ongoogle(query):
if "search" in query:
query = query.replace("search", "")
query = query.replace("on google", "")
webbrowser.open(f"https://www.google.com/search?q={query}")
def google():
webbrowser.open("google.com")
def yahoo():
webbrowser.open("yahoo.com")
def music():
music_dir = 'your path'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
def musicyt():
webbrowser.open(
"https://www.youtube.com/watch?v=gvyUuxdRdR4&list=RDCLAK5uy_n9Fbdw7e6ap-98_A-
8JYBmPv64v-Uaq1g&start_radio=1")
def thetime():
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
def vscode():
codePath = "your path"
os.startfile(codePath)
def pycharm():
codePath = "your path"
os.startfile(codePath)
if __name__ == "__main__":
wishMe()
while True:
# if 1:
query = takeCommand().lower()
if 'wikipedia' in query:
wikipedia(query)
elif 'open youtube' in query:
youtube()
elif 'on google' in query:
ongoogle(query)
elif 'open google' in query:
google()
elif 'open yahoo' in query:
yahoo()
elif 'play music' or 'play song' or 'play songs' in query:
music()
elif 'play music on youtube' or 'play song on youtube' or 'play songs on youtube' in query:
musicyt()
elif 'the time' in query:
thetime()
elif 'open vscode' in query:
vscode()
elif 'open pycharm' in query:
pycharm()
行
elif 'play music' or 'play song' or 'play songs' in query:
估计不是你想的那样。前两个条件只计算字符串 'play music'
和 'play song'
。只有条件的第三部分检查值是否在 query
.
中
您需要检查每个字符串是否在 query
变量中。
elif 'play music' in query or 'play song' in query or 'play songs' in query:
这段代码有问题,我不知道为什么。它一次又一次地执行相同的 elif 语句,没有任何输入和 return desktop.ini 文件,请告诉我如何解决这个问题以及为什么会这样。如果我只放置 5 或 6 个 elif 语句,那么它可以正常工作,但在第 6 个语句之后它就不起作用或开始行为不端。 desktop.ini 文件代码为
[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21790
InfoTip=@%SystemRoot%\system32\shell32.dll,-12689
IconResource=%SystemRoot%\system32\imageres.dll,-108
IconFile=%SystemRoot%\system32\shell32.dll
IconIndex=-237
import pyttsx3
import wikipedia
import speech_recognition as sr
import os
import webbrowser
import datetime
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if 0 <= hour < 12:
speak("Good Morning!")
elif 12 <= hour < 18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("I am your assistant Sir. Please tell me how may I help you")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
# print(e)
print("Say that again please...")
return "None"
return query
def wikipedia(query):
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
def youtube():
webbrowser.open("youtube.com")
def ongoogle(query):
if "search" in query:
query = query.replace("search", "")
query = query.replace("on google", "")
webbrowser.open(f"https://www.google.com/search?q={query}")
def google():
webbrowser.open("google.com")
def yahoo():
webbrowser.open("yahoo.com")
def music():
music_dir = 'your path'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
def musicyt():
webbrowser.open(
"https://www.youtube.com/watch?v=gvyUuxdRdR4&list=RDCLAK5uy_n9Fbdw7e6ap-98_A-
8JYBmPv64v-Uaq1g&start_radio=1")
def thetime():
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
def vscode():
codePath = "your path"
os.startfile(codePath)
def pycharm():
codePath = "your path"
os.startfile(codePath)
if __name__ == "__main__":
wishMe()
while True:
# if 1:
query = takeCommand().lower()
if 'wikipedia' in query:
wikipedia(query)
elif 'open youtube' in query:
youtube()
elif 'on google' in query:
ongoogle(query)
elif 'open google' in query:
google()
elif 'open yahoo' in query:
yahoo()
elif 'play music' or 'play song' or 'play songs' in query:
music()
elif 'play music on youtube' or 'play song on youtube' or 'play songs on youtube' in query:
musicyt()
elif 'the time' in query:
thetime()
elif 'open vscode' in query:
vscode()
elif 'open pycharm' in query:
pycharm()
行
elif 'play music' or 'play song' or 'play songs' in query:
估计不是你想的那样。前两个条件只计算字符串 'play music'
和 'play song'
。只有条件的第三部分检查值是否在 query
.
您需要检查每个字符串是否在 query
变量中。
elif 'play music' in query or 'play song' in query or 'play songs' in query: