0

Trying to build an AI and everything was working until I try to call date(). Am I calling it correctly? Thank you greatly for your help

import pyttsx3
import datetime

engine = pyttsx3.init()

def speak(audio):
    engine.say(audio)
    engine.runAndWait()
    
def time():
    Time = datetime.datetime.now().strftime("%I:%M:%S")
    speak(Time)
    
def date():
    year = int(datetime.datetime.now().year)
    month = int(datetime.datetime.now().month)
    date = int(datetime.datetime.now().day)
    speak(date)
    speak(month)
    speak(year)
date()

Error

2021-10-06 00:17:55.153 python[29408:516608] -[OC_BuiltinPythonNumber length]: unrecognized selector sent to instance 0x7fab3f721d20
^CTraceback (most recent call last):
  File "Jarvis.py", line 21, in <module>
    date()
  File "Jarvis.py", line 18, in date
    speak(date)
  File "Jarvis.py", line 8, in speak
    engine.runAndWait()
  File "/opt/anaconda3/lib/python3.8/site-packages/pyttsx3/engine.py", line 180, in runAndWait
    self.proxy.runAndWait()
  File "/opt/anaconda3/lib/python3.8/site-packages/pyttsx3/driver.py", line 192, in runAndWait
    self._driver.startLoop()
  File "/opt/anaconda3/lib/python3.8/site-packages/pyttsx3/drivers/nsss.py", line 35, in startLoop
    AppHelper.runConsoleEventLoop()
  File "/opt/anaconda3/lib/python3.8/site-packages/PyObjCTools/AppHelper.py", line 263, in runConsoleEventLoop
    if not runLoop.runMode_beforeDate_(mode, nextfire):
KeyboardInterrupt

It hangs at first so I have to use exit with control c

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
May Mammaz
  • 61
  • 1
  • 3
  • 1
    You are stuck in `engine.runAndWait()`, not date. You could comment out the datetime stuff and hard code `year`, etc. See if you have the same problem. Then you can narrow down the problem. – tdelaney Oct 06 '21 at 05:03
  • 1
    Your date is an int, but you should "speak" strings. Don't know if that is the main problem though. – VPfB Oct 06 '21 at 06:01
  • as @VPfB commented, [the docs](https://pyttsx3.readthedocs.io/en/latest/engine.html#pyttsx3.engine.Engine.say) clearly specify that the input to the engine's `say` method must be a string. So for example you could make a date like `Date = datetime.datetime.now().strftime("%A %B %Y")` and then `speak(Date)`. – FObersteiner Oct 06 '21 at 06:41
  • @MrFuppes I seriously cant thank enough!!!! Im just staring with python and some things I still dont understand. Thank you for your time and help everyone. Truly grateful – May Mammaz Oct 06 '21 at 12:58

0 Answers0