這段代碼行為不正常,我不知道為什么。它一次又一次地執行相同的elif陳述句,沒有任何輸入,并回傳desktop.ini檔案,請告訴我怎樣才能解決這個問題,為什么會發生這種情況。如果我只放5或6個elif陳述句,那么它的作業是正常的,但在第6個陳述句之后,它就不作業了或開始行為不正常。 desktop.ini檔案的代碼是
。[.ShellClassInfo] 。
LocalizedResourceName=@%SystemRoot%system32shell32.dll,-21790。
InfoTip=@%SystemRoot%system32shell32.dll,-12689。
IconResource=%SystemRoot%system32imageres.dll,-108
IconFile=%SystemRoot%system32shell32.dll
IconIndex=-237
import pyttsx3
import wikipedia
import speech_recognition as sr
import os
import webbrowser
import datetime
引擎 = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio)。
引擎.say(音頻)
引擎.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("我是您的助理,先生。請告訴我,我可以如何幫助你")
def takeCommand()。
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening..."/span>)
r.pause_threshold = 1
音頻 = r.listen(source)
try:
print("識別...")
query = r.recognize_google(audio, language='en-in')
print(f "用戶說。{query}
")
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", "" )
結果 = wikipedia.summary(query, sentences=2)
speak("根據維基百科")
print(results)
說(結果)
def youtube()。
webbrowser.open("youtube.com"/span>)
def ongoogle(query)。
if "search" in query:
query = query.replace("search", "" )
query = query.replace("on google"/span>, "")
webbrowser.open(f "https://www.google.com/search?q={query}"/span>)
def google()。
webbrowser.open("google.com"/span>)
def yahoo()。
webbrowser.open("yahoo.com"/span>)
def music()。
music_dir = '你的路徑'。
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"/span>)
def thetime()。
strTime = datetime.datetime.now().strftime("%H:%M:%S"/span>)
speak(f "先生,時間是{strTime}")
def vscode()。
codePath = "你的路徑"。
os.startfile(codePath)
def pycharm()。
codePath = "你的路徑"。
os.startfile(codePath)
if __name__ == "__main__"/span>:
wishMe()
while True:
# if 1:.
query = takeCommand().lower()
if 'wikipedia' in查詢。
wikipedia(query)
elif 'open youtube'/span> in query:
youtube()
elif 'on google'/span> 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'/span> or 'play song on youtube' or 'play songs on youtube' in query:
musicyt()
elif 'the time'/span> in query:
thetime()
elif 'open vscode' in query:
vscode()
elif 'open pycharm' in query:
pycharm()
uj5u.com熱心網友回復:
該行
elif 'play music' 或 'play song' 或 'play songs' in query。
可能不是按照你所想的方式評估的。前兩個條件只是評估為字串'play music'和'play song'。只有條件的第三部分在檢查query中是否有一個值。
你需要檢查每個字串是否在query變數中。
elif 'play music' in query 或 'play song' in query 或 'play songs' in query:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/314495.html
標籤:
上一篇:如何在沒有echo的情況下在變數中分配運算式的字串輸出?[復制]
下一篇:同一公式中的IFAND和IFOR
