我试图在这个程序中插入一个while循环,所以当它要求成员时它会启动一个字典(假设我会使用一个字典),它允许我输入n个乐队成员和他们以某种形式使用的乐器(名称,仪器),(名称2,仪器2)..等等。我知道它将作为while循环运行,但我不确定如何在此实例中输入它。使用字典是最好的动作,如果是这样,我会如何在这样的情况下添加它?
class Song:
def __init__(self, song, chart, member):
self.song = song
self.chart = chart
self.member = member
def __str__(self):
return (self.song
+ " Top Chart:"
+ str(self.chart)
+ " Band Members: "
+ str(self.member)
+ '\n')
def write():
with open("string.txt", "a+", encoding="utf-8") as f:
while(1):
prompt = ":\t"
in_song = input("Input song or type q " + prompt)
if (in_song == 'q'):
break
in_chart_spot = input("Input chart spot" + prompt)
in_band_mem = input("Input band members" + prompt)
song_data = Song(in_song, in_chart_spot, in_band_mem)
#albumdata = Album()
f.write(str(song_data))
if __name__ == '__main__':
write()