我按下键盘上的某个按钮,现在我收到了with语句的语法错误,该语句在之前工作正常

2024-03-28 08:50:10 发布

您现在位置:Python中文网/ 问答频道 /正文

import speech_recognition as sr
import pyttsx3
import datetime

r = sr.Recognizer()
mic = sr.Microphone()
engine = pyttsx3.init()
engine.setProperty('rate',120)  #75 words per minute
engine.setProperty('volume',0.9) #loudness of speaker

def getTime():
    str((datetime.datetime.now())

with mic as source:
    audio = r.listen(source)

myInput = r.recognize_google(audio)

responses = {"hello":"hello there", "thanks":"don't mention it", "time":getTime}

if myInput in responses.keys():
    engine.say(responses[myInput])
    engine.runAndWait()

else:
    engine.say("Sorry I don't understand that")
    engine.runAndWait()

嘿,伙计们,我对python真的是个新手,我只是在搞这个。在我按下键盘上的CTRL+Z键撤销某些操作之前,一切都很顺利。我很确定我错过了Z,从那以后,我得到了with语句的语法错误

知道我到底做了什么吗


Tags: importsourcehellodatetimeaswithresponsesaudio