Am I being “not smart”.
why is the split function not working
It just returns an empty list.
Am I being “not smart”.
why is the split function not working
It just returns an empty list.
It looks like it’s giving an error from a place you can’t control, since you’re not using “length” in your program.
Cannot read property ‘length’ of undefined
at (main.ts:1:1)
It works in JavaScript…
Yes I know but it should work in python
.split(" ") works the same in both languages,
but in python for some reason it is returning []
which the obviously
test[1]
returns an error
can this be fixed?
Yeah, it should. If it still doesn’t work on Monday, I’ll check in with the team.
The split is still not working. What’s wrong with using it?
I don’t think Python currently supports split()
on strings. I’ve opened an “issue” on GitHub to report the error. See here: https://github.com/microsoft/pxt/issues/8752
In the meantime, you could use a function to split strings by a single character. Here’s an example (I’ve tested it in the simulator, but not on a micro:bit):
words = "this is a test"
def simpleSplit(string, char):
array = []
current = ""
for c in string:
if c==char:
array.push(current)
current = ""
else:
current = current + c
if current != "":
array.push(current)
return array
splitBySpace = simpleSplit(words, " ")
serial.write_number(len(splitBySpace))
serial.write_line("")
for w in splitBySpace:
serial.write_line(w)
The GitHub issue has been closed. It looks like split()
in Python may now work: https://github.com/microsoft/pxt/issues/8752#issuecomment-1198700037 .