循环时在后台进行循环



所以我在这里混淆了

while self.voice_client.is_playing() == True:
self.time += 1
self.spot.clear()
for item in self.spot:
video = search(query=item)
self.query.append(video)

如何将这两部分结合起来?我希望for loopwhile loop同时工作。当self.item += 1时,for item...运行得太

使循环在后台循环时工作

通过对for循环的功能进行编程,将for循环的功能置于while循环中。

for循环处理可迭代项,直到项结束。项目的末尾由StopIteration错误标识,该错误导致退出for循环。

要复制代码中的功能,请确保忽略StopIteration并继续外部循环。

# Build for loop functionality
self_spot = ['Song_1','Song_2']
self_spot_items = iter(self_spot) # The iterable part of a for loop
count = 0 #
while True:
count += 1
# For loop functionality
try:
print(f'{next(self_spot_items)=}')
# video = search(query=item)
# self.query.append(video)
except StopIteration: # item processing done
pass # continue the while loop
# Outer loop functionlaity
print(f'{count=}')
if count > 10:
break

输出

next(self_spot_items)='Song_1'
count=1
next(self_spot_items)='Song_2'
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
count=11

最新更新