我有一个带有循环变量i
的for
语句,每次自然增加1。但是,当我运行for
语句时,有一行获取某个列表的索引i
,它会做一些奇怪的事情。所发生的是,它返回的正确值很少,其他值似乎是随机的。你知道吗
def allchords(thescale,issev):
for i in range(len(thescale)):
makechord((thescale[i]),thescale,issev)
当i=0
时,它会正确返回thescale[0]
。你知道吗
当i=1
时,它会正确返回thescale[1]
。你知道吗
当i=2
时,它出于某种原因返回thescale[3]
。你知道吗
当i=3
时,它返回thescale[6]
当i=4
时,它返回thescale[3]
当i=5
时,它返回thescale[1]
当i=6
时,它返回thescale[0]
到底怎么回事?你知道吗
好的,下面是整个makechord
函数:
def makechord(tnc,thescale,issev):
crdscl=thescale
for i in range(len(thescale)):
if crdscl[0] == tnc:
break
else:
tomove=crdscl[0]
crdscl.pop(0)
crdscl.append(tomove)
if issev == "y" or "Y":
thecrd=[(crdscl[0]),(crdscl[2]),(crdscl[4]),(crdscl[6])]
else:
thecrd=[(crdscl[0]),(crdscl[2]),(crdscl[4])]
print thecrd
您确实在
thescale
内修改makechord()
:只需将名称
crdscl
分配给thescale
,以便以后调用实际上,您正在修改
thescale
的内容。避免这种情况的一个简单方法是将内容的副本thescale
分配给crdscl
:相关问题 更多 >
编程相关推荐