1

I want my raspberry pi to speak this print command

print label + " is " + spos + " and the distance is " + str(distance) + "cm"

which has three variables in it. And this is the command i use in shell that i need to use in python

$espeak -s110  "label + " is " + spos + " and the distance is " + str(distance) + "cm"" --stdout | aplay -D sysdefault:CARD=2

I've tried it with os.system because i'm not familiar with subprocess commands.

os.system('espeak -s110  "'label' + is + 'spos' +  and the distance is  + 'str(distance)' + cm" --stdout | aplay -D sysdefault:CARD=2')

I'm getting an invalid sytax error. I've tried every version of it and couldn't make it work.

Silver Fox
  • 13
  • 3
  • You have a syntax error in how you are trying to assemble the string for `os.system`. Think about where the `'` and `"` should be and how string concatenation works. Alternatively, see the [documentation for subprocess](https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module) and give it a try. – Galen Dec 11 '17 at 11:37

1 Answers1

1

This should give you what you're looking for:

cmd = 'espeak -s110 "{0} is {1} and the distance is {2} cm" --stdout | aplay -D sysdefault:CARD=2'.format(label, spos, str(distance))

os.system(cmd)
mij
  • 532
  • 6
  • 13