Python TypeError:convert_to_audio()接受2个位置参数,但给出了3个
所以,我只是Pythonl的一个业余爱好者,就像我只是一个第12标准的初学者一样,我得到了一个程序来创建一个智能时钟,它会告诉你现在是什么时间,我必须使用datetime、GTT和playsound模块来制作这个程序。所以,我创建了一个非常基本的程序,但它显示的错误是TypeError:convert_to_audio接受了2个位置参数,但给出了3个。代码是:Python TypeError:convert_to_audio()接受2个位置参数,但给出了3个,python,Python,所以,我只是Pythonl的一个业余爱好者,就像我只是一个第12标准的初学者一样,我得到了一个程序来创建一个智能时钟,它会告诉你现在是什么时间,我必须使用datetime、GTT和playsound模块来制作这个程序。所以,我创建了一个非常基本的程序,但它显示的错误是TypeError:convert_to_audio接受了2个位置参数,但给出了3个。代码是: from datetime import datetime from gtts import gTTS from playsound i
from datetime import datetime
from gtts import gTTS
from playsound import playsound
now = datetime.now()
current_time = now.strftime("%H:%M:%S %p")
print("Current Time =", current_time)
def playaudio(audio):
playsound(audio)
def convert_to_audio(self,text):
audio=gTTS(text)
audio.save("Textaudio.mp3")
playaudio("textaudio.mp3")
convert_to_audio("Hello Ravi. Good Morning the time is",current_time,"Have a nice day")
错误消息非常清楚地说明了问题:您正在传递三个参数,即: 你好,拉维。早上好,时间到了 当前时间 祝你今天愉快 将_转换为_音频功能,而它只需要两个,即: 自己 文本 您必须删除其中一个参数才能消除该错误
如果我理解正确,您要做的是传递一个包含当前时间的字符串。看一看Python的字符串函数来解决您的问题。那么,你也可以摆脱多余的自我辩论。看起来你犯了一些错误,这是可以的。您仍在学习,但在编写代码之前,请始终找出问题陈述,并寻找没有代码的解决方案。一旦你做到了这一点,python只是实现事物的另一种方式 尝试调试并找出发生了什么
from datetime import datetime
from gtts import gTTS
from playsound import playsound
def playaudio(audio):
playsound(audio)
def convert_to_audio(text):
audio=gTTS(text)
audio.save("Textaudio.mp3")
playaudio("Textaudio.mp3")
now = datetime.now()
current_time = now.strftime("%H:%M:%S %p")
convert_to_audio("Hello Ravi. Good Morning the time is {}. Have a nice day").format(current_time)
@Raghav Gupta谢谢老兄我最终使用了以下代码:-
from datetime import datetime
from gtts import gTTS
from playsound import playsound
now = datetime.now()
current_time = now.strftime("%H:%M:%S %p")
print("Current Time =", current_time)
def playaudio(audio):
playsound(audio)
def convert_to_audio(Text):
audio=gTTS(Text)
audio.save("Textaudio.mp3")
playaudio("textaudio.mp3")
strtime=str(current_time)
convert_to_audio("Hello Ravi. Good Morning the time is"+strtime+"Have a nice day")
错误消息是否不够清楚?函数需要2件东西,文本,但得到3件东西Hello Ravi。早上好时间是,当前时间,祝您愉快为什么函数中甚至有一个self参数?您是否从一个示例中删除了该代码,其中该函数是一个类的方法?你至少应该试着理解你在做什么。@rdas那么我如何向函数中添加3项内容呢?我认为self应该作为一个参数删除,输入应该是一个字符串。@Matthias但是如果它是一个字符串,那么GTT和playsound模块将如何告诉当前时间?@Warmachine你读过Raghav的答案了吗古普塔?你知道你可以组合字符串来创建一个新字符串吗?@Matthias谢谢你,伙计,我现在得到了。很高兴我能帮上忙!也要对被接受的答案进行投票,以吸引更多的观众。