Python 创建函数列表?
我正在开发一个乐谱应用程序,现在我需要创建一个列表或任何可以让我这样做的东西,将所有这些信息存储为一个项目,然后打印出来,或者更好地将其插入到我的代码中,由一组函数操作Python 创建函数列表?,python,loops,while-loop,Python,Loops,While Loop,我正在开发一个乐谱应用程序,现在我需要创建一个列表或任何可以让我这样做的东西,将所有这些信息存储为一个项目,然后打印出来,或者更好地将其插入到我的代码中,由一组函数操作 print('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()') 所有这些打印出来的东西都像这样 Note(8,
print('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()')
所有这些打印出来的东西都像这样
Note(8, '4R', 4, 'c', 'Ethnote').ExNote()
当硬编码到我的代码中时,通过这些类函数将第八个音符打印到我的乐谱上
class Note:
def __init__(self, Num, staff, measure, note, notetype):
self.staff = staff
self.measure = measure
self.note = note
self.notetype = notetype
self.Num = Num
def Wmeasure(self):
return (self.measure-1)*153
def Wnotetype(self):
if self.notetype == 'Ethnote':
X= {'1':x+5, '2':x+22, '3':x+39, '4':x+56, '5':x+73, '6':x+90, '7':x+107, '8':x+124}
elif self.notetype == 'Fourthnote':
X={'1':x+19, '2':x+50, '3':x+81, '4':x+112}
elif self.notetype == 'Halfnote':
X={'1':x+39, '2':x+90}
elif self.notetype == 'note1':
X={'1':x+64, '2': x+64}
return X[str(self.Num)]
def Wnote(self):
YL={'b': y+76, 'a': y+80, 'g':y+84, 'f':y+88, 'e':y+92, 'd':y+96, 'c':y+100, 'b2':y+104, 'a2':y+108, 'a3': y+112}
YR= {'c': 62, 'd': 58, 'e': 54, 'f': 50, 'g':46, 'a':42, 'b':38,
'c2':34, 'd2':28 , 'e2':24, 'f2':20, 'g2':16, 'a2':12, 'b2':8, 'c3':4, 'd3':0}
if self.staff in ['1L', '2L', '3L', '4L']:
#self.staff == '1L': # or '2L' or '3L' or '4L':
return YL[self.note] #+ self.Wstaff()
else: #if self.staff == '1R' or '2R' or '3R' or '4R':
return YR[self.note] #+ self.Wstaff()
def Wstaff(self):
if self.staff in ['1L', '1R']:
j = 0
elif self.staff in ['2L', '2R']:
j = 160
elif self.staff in ['3L', '3R']:
j = 320
elif self.staff in ['4L', '4R']:
j = 480
return j
def getcoord(self):
return (self.Wmeasure() + self.Wnotetype()), (self.Wstaff() + self.Wnote())
def ExNote(self):
if self.notetype == 'Ethnote':
screen.blit(EthnoteIMG, self.getcoord())
elif self.notetype == 'Fourthnote':
screen.blit(FourthnoteIMG, self.getcoord())
elif self.notetype == 'Halfnote':
screen.blit(HalfnoteIMG, self.getcoord())
elif self.notetype == 'note1':
screen.blit(note1IMG, self.getcoord())
所以我下一步要做的是列一个清单或者其他东西来存储这个
('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()')
。。。作为一个项目,然后我必须创建一个函数,该函数接受列表中的所有项目,并以某种方式将它们插入到我的代码中,因为仅仅将它们打印出来不会起任何作用。
好的,我尝试了这个方法,它并没有解决整个问题,但肯定会让我更接近,但它不起作用,我不知道为什么。我在一个单独的文件中对其进行了测试,因为这样更容易,而且没有错误或任何东西
如果我理解你的意思,是的,我读了你的另一个问题,但我更喜欢这个问题。你想要一个符号的文本表示,你可以很容易地转换和转换。如果这是正确的,那么我建议大致如下:
from note import Note
import jsonpickle
note = Note(8, '4R', 4, 'c', 'Ethnote')
text = jsonpickle.encode(note)
print(text)
object = jsonpickle.decode(text)
object.ExNote()
输出
> python3 test.py
{"py/object": "note.Note", "Num": 8, "measure": 4, "note": "c", "notetype": "Ethnote", "staff": "4R"}
...
然后是一堆错误,因为我没有加载Pygame或者其他你用来做screen.blit的东西
您可以自定义jsonpickle,从JSON的角度来决定编码内容/方式。我添加了一些基本格式,并将图像内联。请将您的问题和代码粘贴为文本,而不是发布屏幕截图。您可以选择它,然后按Ctrl+K或单击{}按钮以正确格式化它。我试过了。{}按钮和Ctrl K什么也不做,它只是说代码的格式不正确,因为它不是故意的,即使它是。@Chris OK!!终于成功了!我把代码放在正常的地方,通常不太适合删除一个原始问题,用不同的东西替换它。如果您还有第二个问题,请单独提交。事实上,这个问题没有任何意义。你没有解释你想做什么,也没有清楚地说出哪里出了问题。你也没有用你正在使用的编程语言来标记这个问题,这比你正在使用几乎所有代码都使用循环的事实要重要得多。@Blckknght我创建了一个新的问题,希望它更容易理解。