bbc-microbit: micropython AttributeError: 'str' object has no attribute 'partition'

bbc-microbit: micropython AttributeError: 'str' object has no attribute 'partition'

在 BBC microbit 上,我收到此错误,我不知道为什么:

AttributeError: 'str' object has no attribute 'partition'

当 运行 此代码时:

uart.write('Received: "' + incoming + '"\n')
head, mid, tail = incoming.partition(' ')

incoming是一个字符串,可以在控制台看到

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>> 
>>> Received: "buggy direction 2.16 1.2"
Traceback (most recent call last):
  File "__main__", line 122, in <module>
  File "__main__", line 25, in drive
AttributeError: 'str' object has no attribute 'partition' 

知道这里可以做什么吗?

BBC micropython字符串class没有分区方法。尝试使用拆分方法。您示例中的字符串 "buggy direction 2.16 1.2" 有四个元素。试图将其拆分为三个变量 head、mid、tail 会导致错误。

您可以使用下面的示例代码访问字符串的第一个和最后一个元素:

words = incoming.split(',')
head = words[0]
tail = words[-1]
print('head: {} tail: {}'.format(head, tail))