我从网上的一篇文章中获取了这段代码,并已经完成了故障排除,现在它以退出码0运行。它什么也不做,既不打招呼也不请求输入,只是运行并给出退出码。
我在安装ecapture时遇到了问题,似乎很多使用Python 3.9的人都遇到了这个问题,所以我已经将其注释掉了。
这是我获取代码的来源。
https://towardsdatascience.com/how-to-build-your-own-ai-personal-assistant-using-python-f57247b4494b
# 按Shift+F10执行它或用你的代码替换它。# 按双Shift键可以在类、文件、工具窗口、操作和设置中搜索。def print_hi(name): # 在下面的代码行中使用断点来调试你的脚本。 print(f'Hi, {name}') # 按Ctrl+F8切换断点。# 按槽中的绿色按钮运行脚本。if __name__ == '__main__': print_hi('PyCharm')# 查看PyCharm帮助,请访问 https://www.jetbrains.com/help/pycharm/import speech_recognition as srimport pyttsx3import datetimeimport wikipediaimport webbrowserimport osimport timeimport subprocess"""from ecapture import ecapture as ec"""import wolframalphaimport jsonimport requestsengine=pyttsx3.init('sapi5')voice=engine.getProperty('voices')engine.setProperty('voice', 'voices[0].id')def speak(text): engine.say(text) engine.runAndWait()def wishMe(): hour=datetime.datetime.now().hour if hour>=0 and hour<12: speak("Hello,Good Morning") print("Hello,Good Morning") elif hour >= 12 and hour <18: speak("Hello, Good Afternoon") print("Hello, Good Afternoon") else: speak ("Hello, Good Evening") print ("Hello, Good Evening") def takeCommand(): r=sr.Recognizer() with sr.Microphone() as source: print ("Listening...") audio=r.listen(source) try: statement=r.recognize_google(audio,language='en-uk') print(f"user said:{statement}\n") except Exception as e: speak ("Run that by me again") return "None" return statement print ("Loading your AI personal assistant G-One") speak ("Loading your AI personal assistant G-One") wishMe() if __name__ == '__main__': while True: speak("How may I be of assistance?") statement = takeCommand().lower() if statement==0: continue if "good bye" in statement or "ok bye" in statement or "that'll be all" in statement: speak ('your personal assistant G-One is shutting down, Good bye') print ('your personal assistant G-One is shutting down, Good bye') if 'wikipedia' in statement: speak('Searching Wikipedia...') statement =statement.replace("wikipedia", "") results =wikipedia.summary(statement, sentences=3) speak ("According to Wikipedia") print (results) speak (results) elif 'open youtube' in statement: webbrowser.open_new_tab("https://www.youtube.com") speak ("youtube is now open") time.sleep(5) elif 'open google' in statement: webbrowser.open_new_tab("https://www.google.com") speak ("Google chrome is now open") time.sleep(5) elif 'open gmail' in statement: webbrowser.open_new_tab("gmail.com") speak ("Gmail is now open") time.sleep(5) elif time in statement: strTime=datetime.datetime.now().strftime("%H:%M:%S") speak(f"the time is {strTime}") elif 'news' in statement: news = webbrowser.open_new_tab("https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRFZxYUdjU0FtVnVHZ0pKVGlnQVAB?hl=en-IN&gl=IN&ceid=IN%3Aen") speak('Here are some headlines from Google news') time.sleep(6) elif 'search' in statement: statement = statement.replace("search","") webbrowser.open_new_tab(statement) time.sleep(5) elif 'ask' in statement: speak('I can answer to computational and geographical questions, which of these would you like to ask now') question=takeCommand() app_id="LURX84-VPJ6LEHWXV" """Wolfram App ID""" client = wolframalpha.Client('R2K75H-7ELALHR35X') res = client.query(question) answer = next(res.results).text speak (answer) print (answer) elif 'who are you' in statement or 'what are your capabilities' in statement: speak ('I am Arthur, and am still in my infancy. I am capable of accomplishing minor tasks at this point in time such as' 'opening google products such as youtube, gmail, and chrome.' 'I have other functions such as relating the time, taking pictures,searching wikipedia, relating the weather' 'as well as reading headlines from google news') elif 'who made you' in statement or 'who built you' in statement or 'who discovered you' in statement: speak("I was built by Ahmed Hamadto") print ("I was built by Ahmed Hamadto") """include the weather part of the AI here""" if 'log off' in statement or 'lights out' in statement: speak ("You've got 10 seconds to clear things up") subprocess.call(["shutdown","/1"]) time.sleep(3)""" elif 'camera'in statement or 'take a photo' in statement or 'snap this' in statement: ec.capture(0,"robo camera","img.jpg")"""
回答:
你的缩进有误。你把if __name__ == "__main__"
放在了wishMe
函数内部。把它移到外面,应该就能正常工作了。