语音识别显示 Error In Python(位置参数错误)
Speech recognition shows Error In Python (Positional Argument Error)
Python 当我 运行 这段代码时出现错误,我已经检查了它很多次。这里的来源是麦克风,但它一直在询问 'source' 的值。我应该怎么办。我应该将什么参数传递给 'source'?
import pyttsx3
import speech_recognition as sr
#Required Modules
engine = pyttsx3.init()
r = sr.Recognizer
#Initializes variable (Avoiding Time Delay)
def Speak(audio):
engine.say(audio)
engine.runAndWait()
#Defining the function Speak() for Audio Output
def Listen():
with sr.Microphone() as source:
r.adjust_for_ambient_noise
print("Listening")
voice = r.listen(source)
print("Done Listening")
a = r.recognize_google(voice)
a = a.lower()
print("You asked - "+a)
#Defining the function Listen() for Audio Input (INTERNET REQUIRED)
while True:
Speak("Ask Me Some Questions")
#Speaks
Listen()
#Listens
try:
if a == "how are you":
print("I'm Fine Sir")
Speak("I'm Fine Sir")
#Tries to do the program without error
except:
print("Error Occured!!!")
#If error occured, this will be the Output
#Looped
此代码出错,
Traceback (most recent call last):
File "c:\Users\Vishal\AppData\Local\Programs\Python\Python39\Python Projects\Speech.py", line 29, in <module>
Listen()
File "c:\Users\Vishal\AppData\Local\Programs\Python\Python39\Python Projects\Speech.py", line 18, in Listen
voice = r.listen(source)
TypeError: listen() missing 1 required positional argument: 'source'
实际上是什么错误以及如何解决? (我是 python 的初学者,请放轻松)
这是更新后的代码
1.You 必须始终首先查看文档 应该 sr.Recognizer()
2.and 如果您正在创建方法,那么对于交互,您应该 return value
import pyttsx3
import speech_recognition as sr
#Required Modules
engine = pyttsx3.init()
r = sr.Recognizer()
#Initializes variable (Avoiding Time Delay)
def Speak(audio):
engine.say(audio)
engine.runAndWait()
#Defining the function Speak() for Audio Output
def Listen():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening")
voice = r.listen(source)
print("Done Listening")
a = r.recognize_google(voice)
a = a.lower()
print("You asked - "+a)
return a
#Defining the function Listen() for Audio Input (INTERNET REQUIRED)
while True:
Speak("Ask Me Some Questions")
#Speaks
a=Listen()
#Listens
try:
if a == "how are you":
print("I'm Fine Sir")
Speak("I'm Fine Sir")
#Tries to do the program without error
except:
print(a)
print("Error Occured!!!")
#If error occured, this will be the Output
#Looped
Python 当我 运行 这段代码时出现错误,我已经检查了它很多次。这里的来源是麦克风,但它一直在询问 'source' 的值。我应该怎么办。我应该将什么参数传递给 'source'?
import pyttsx3
import speech_recognition as sr
#Required Modules
engine = pyttsx3.init()
r = sr.Recognizer
#Initializes variable (Avoiding Time Delay)
def Speak(audio):
engine.say(audio)
engine.runAndWait()
#Defining the function Speak() for Audio Output
def Listen():
with sr.Microphone() as source:
r.adjust_for_ambient_noise
print("Listening")
voice = r.listen(source)
print("Done Listening")
a = r.recognize_google(voice)
a = a.lower()
print("You asked - "+a)
#Defining the function Listen() for Audio Input (INTERNET REQUIRED)
while True:
Speak("Ask Me Some Questions")
#Speaks
Listen()
#Listens
try:
if a == "how are you":
print("I'm Fine Sir")
Speak("I'm Fine Sir")
#Tries to do the program without error
except:
print("Error Occured!!!")
#If error occured, this will be the Output
#Looped
此代码出错,
Traceback (most recent call last):
File "c:\Users\Vishal\AppData\Local\Programs\Python\Python39\Python Projects\Speech.py", line 29, in <module>
Listen()
File "c:\Users\Vishal\AppData\Local\Programs\Python\Python39\Python Projects\Speech.py", line 18, in Listen
voice = r.listen(source)
TypeError: listen() missing 1 required positional argument: 'source'
实际上是什么错误以及如何解决? (我是 python 的初学者,请放轻松)
这是更新后的代码
1.You 必须始终首先查看文档 应该 sr.Recognizer()
2.and 如果您正在创建方法,那么对于交互,您应该 return value
import pyttsx3
import speech_recognition as sr
#Required Modules
engine = pyttsx3.init()
r = sr.Recognizer()
#Initializes variable (Avoiding Time Delay)
def Speak(audio):
engine.say(audio)
engine.runAndWait()
#Defining the function Speak() for Audio Output
def Listen():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening")
voice = r.listen(source)
print("Done Listening")
a = r.recognize_google(voice)
a = a.lower()
print("You asked - "+a)
return a
#Defining the function Listen() for Audio Input (INTERNET REQUIRED)
while True:
Speak("Ask Me Some Questions")
#Speaks
a=Listen()
#Listens
try:
if a == "how are you":
print("I'm Fine Sir")
Speak("I'm Fine Sir")
#Tries to do the program without error
except:
print(a)
print("Error Occured!!!")
#If error occured, this will be the Output
#Looped