Problem mit split Funktion in Python

Hallo,
Ich möchte ein Musik-Programm auf dem Calliope-mini programmieren

Ich habe ein String mit meine Noten, den ich in ein Array umwandeln möchte.
Dann diese Array kann in der Funktion „music.start_melody“ verstanden werden.

Hier ist mein Code :

# Input string melody
stringMusic = "B:3,D:4,CSHARP:3"
print("String : " + stringMusic)

arrayMusic = stringMusic.split(",")
print("Array : " + arrayMusic)
print("Array element 0 : " + arrayMusic[0])

# Output wanted ["B:3", "D:4", "CSHARP:3"]

Hier ist das Simulator console :

String : B:3,D:4,CS:3
Array : [object Object]
Array element 0 : undefined

In diesem Kontext, die „split“ Funktion gibt „undefined“ zurück und nicht das Array
Ich glaube ich habe keine Fehler in Python gemacht

Habt ihr schon einmal diese Problem gehabt?

Vielen Dank
Alexandre

PS : Ich lerne noch Deutsch, ich hoffe ihr versteht mir :slight_smile:

Hallo AlexandreM,

folgendes funktioniert bei mir:

feld: List[str] = []
stringMusic = "B:3,D:4,CSHARP:3"
feld = stringMusic.split(",")
Index = 0
while Index <= len(feld):
    music.play_tone(262, music.beat(BeatFraction.SIXTEENTH))
    basic.show_string("" + (feld[Index]))
    basic.pause(2000)
    Index += 1

oder mit Blöcken:

Die Schleife untern dient nur der Ausgabe. Verzichte ich in deinem Quelltext auf print, klappt es auch.

HG Toni

Hallo Toni,
Danke für deine Antwort! :slight_smile:

Ich habe deine Python code auf meine code kopiert, aber das funktioniert nicht… („undefined“ rührkehrt). Normalerweise ich codiert im Python direkt und ich nicht der Blöcken verwenden

So habe ich deine Blöcke in ein neues Projekt kopiert, und das funktioniert, aber wenn ich den Python code direkt in ein neues Projekt kopiere, das funktioniert nicht…
Ich habe auch an eine Calliope Mini REV2 (letzte bootloader) getestet, gleiches Verhalten…

Villeicht gibt es eine code Umwandlung zwichen Python und Typescript (JavaScript) problem? Manchmal habe ich kleine Problem mit Variable setzen wenn ich mit Python programmiere…

Vielen Dank
Alexandre

Hallo Alexandre,

ich kann den Fehler replizieren. Er entsteht auch im Microbit-Editor. Leider kann ich da nicht helfen. Meine Python-Kenntnisse sind sehr oberflächlich.

Toni