1

我想让我的树莓派说出这个打印命令

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

其中包含三个变量。这是我在 shell 中使用的命令,我需要在 python 中使用

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

我已经用 os.system 试过了,因为我不熟悉subprocess命令。

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

我收到无效的语法错误。我已经尝试了它的每个版本,但无法使其正常工作。

4

1 回答 1

1

这应该给你你正在寻找的东西:

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)
于 2017-12-11T11:37:54.947 回答