Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用if语句和一个for循环来运行它?_Python - Fatal编程技术网

Python 如何使用if语句和一个for循环来运行它?

Python 如何使用if语句和一个for循环来运行它?,python,Python,我试图通过使用if语句、for循环和列表来运行它。该列表是参数的一部分。我不知道如何编写if语句,让程序循环遍历所有不同的单词,并设置它应该是什么样子 newSndIdx=0; for i in range (8700, 12600+1): sampleValue=getSampleValueAt(sound, i) setSampleValueAt(newSnd, newSndIdx, sampleValue) newSndIdx +=1 newSndIdx=

我试图通过使用if语句、for循环和列表来运行它。该列表是参数的一部分。我不知道如何编写if语句,让程序循环遍历所有不同的单词,并设置它应该是什么样子

newSndIdx=0;
  for i in range (8700, 12600+1):
    sampleValue=getSampleValueAt(sound, i)
    setSampleValueAt(newSnd, newSndIdx, sampleValue)
    newSndIdx +=1

  newSndIdx=newSndIdx+500
  for i in range (15700, 17600+1):
    sampleValue=getSampleValueAt(sound, i)
    setSampleValueAt(newSnd, newSndIdx, sampleValue)
    newSndIdx +=1

  newSndIdx=newSndIdx+500    
  for i in range (18750, 22350+1):
    sampleValue=getSampleValueAt(sound, i)
    setSampleValueAt(newSnd, newSndIdx, sampleValue)
    newSndIdx +=1

  newSndIdx=newSndIdx+500    
  for i in range (23700, 27250+1):
    sampleValue=getSampleValueAt(sound, i)
    setSampleValueAt(newSnd, newSndIdx, sampleValue)
    newSndIdx +=1

  newSndIdx=newSndIdx+500    
  for i in range (106950, 115300+1):
    sampleValue=getSampleValueAt(sound, i)
    setSampleValueAt(newSnd, newSndIdx, sampleValue)
    newSndIdx+=1
关于(如果需要,则否):


我想我知道你在找什么。如果是这样,那就相当笨拙了;GaretJax重新设计它的方式更简单、更清晰(而且启动时效率更高)。但这是可行的:

# Put the ranges in a list:
ranges = [
    (8700, 12600),
    (15700, 17600),
    (18750, 22350),
    (23700, 27250),
    (106950, 115300),
]

newSndIdx = 0

# Write a single for loop over the whole range:
for i in range(number_of_samples):
    # If the song is in any of the ranges:
    if any(r[0] <= i <= r[1] for r in ranges):
        # Do the work that's the same for each range:
        sampleValue=getSampleValueAt(sound, i)
        setSampleValueAt(newSnd, newSndIdx, sampleValue)
        newSndIdx +=1

请再具体一点。你的问题是什么?你对你的计划有什么期望?这描述太模糊了。它还没有运行吗?为什么需要使用
if
for loop
list
?它们是干什么用的?这些
不同的词是什么?您需要设置什么?这已经起作用了,但我试图将其压缩为if语句和for循环语句。for循环将在所有不同的范围内运行,并完全执行此程序已经执行的操作。谢谢,这是可行的,但它只播放一个单词(我可以听到),我想您需要的是
newSndIdx+=500
,而不是
=500
。这样,它会用每个新段覆盖轨迹的相同部分,而不是在段之间留下间隙。
# Put the ranges in a list:
ranges = [
    (8700, 12600),
    (15700, 17600),
    (18750, 22350),
    (23700, 27250),
    (106950, 115300),
]

newSndIdx = 0

# Write a single for loop over the whole range:
for i in range(number_of_samples):
    # If the song is in any of the ranges:
    if any(r[0] <= i <= r[1] for r in ranges):
        # Do the work that's the same for each range:
        sampleValue=getSampleValueAt(sound, i)
        setSampleValueAt(newSnd, newSndIdx, sampleValue)
        newSndIdx +=1
    if any(r[0] <= i <= r[1] for r in ranges):
        if any(r[0] == i for r in ranges[1:]):
            newSndIdx += 500
        # The other stuff above