0

hello everyone i have write code and it keep getting an error can someone tell me what is wrong with the code?

file = open("mypiano.txt", 'r+')
slist = []
for line in file.readlines():
    value = line.split()
    slist.append([value[0],value[1],value[2]])
print(slist)
file.close()
#print(type(slist))

twinkle_list = slist
#print(type(twinkle_list))

from threading import Thread
import pygame as pg
import time

pg.mixer.init()
pg.init()

pg.mixer.set_num_channels(len(twinkle_list))
def play_notes(notePath,duration):
    time.sleep(duration)
    pg.mixer.Sound(notePath).play()
    time.sleep(duration)
    print(notePath)

path = ("Sounds/")
cnt = 1
th = {}
for t in twinkle_list :
    th[t] = Thread(target = play_notes,args = (path+'{twinkle_list}.ogg'.format(t),0.3))
    th[t].start()
    th[t].join()
    if cnt%7==0:
        time.sleep(1)
    cnt +=1

the error said like this : Traceback (most recent call last): File "C:/Users/wmeyl/PycharmProjects/DSP/venv/piano_test1.py", line 31, in th[t] = Thread(target = play_notes,args = (path+'{twinkle_list}.ogg'.format(t),0.3)) KeyError: 'twinkle_list'

  • Where you wrote `'{twinkle_list}.ogg'.format(t)`, what do you want that to do? – Karl Knechtel Dec 14 '21 at 07:39
  • Does https://stackoverflow.com/questions/517355/string-formatting-in-python help, as a reference? – Karl Knechtel Dec 14 '21 at 07:41
  • im trying to read the twinkle_list = slist and that why i put it inside the '{twinkle_list}.ogg'.format(t), but i got the error th[t] = Thread(target = play_notes,args = (path+'{twinkle_list}.ogg'.format(t),0.3)) KeyError: 'twinkle_list' – Meylan Wongkar Dec 14 '21 at 08:00
  • Okay, but *specifically the part that says* `'{twinkle_list}.ogg'.format(t)`, when that runs, *exactly what do you think should happen as a result*, and *why*? – Karl Knechtel Dec 15 '21 at 02:05

0 Answers0