So I confused at here
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)
How to combine this two parts? I want that for loop
is working same time as while loop
. While self.item = 1
, that for item...
is running too
CodePudding user response:
Making for loop working in the background while loop
Place the functionality of a for
loop in the a while
loop by programming the functionality of a for
loop.
A for
loop processes an iterable until the end of the items. The end of the items is identified by a StopIteration
error which causes exiting from the for
loop.
To replicate the functionality in code, make sure to ignore StopIteration
and continue the outer loop.
# 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
Output
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