随机列表项,但无法两次选择相同的项目
Random list item, but unable to choose the same item twice
是否可以 运行 一个循环,它选择一个随机列表项,但不能连续两次选择相同的项。
我已经尝试 运行 在一个线程中使用 Clock.schedule_interval
我不确定如何同时保持循环和 GUI 运行ning。
当间隔结束时,它会“重置”循环。因此可以再次选择相同的项目。
def toggle(self):
self.start()
i = self.root.ids.interval.text #interval
i = int(i)
Clock.schedule_interval(self.playsound, i)
def playsound(self, *args):
while True:
sounds = [1,2,3,4]
t = random.randrange(len(sounds)-1)
sounds.append(sounds.pop(t))
x = (sounds[-1])
if x == 1:
print("1")
elif x == 2:
print("2")
elif x == 3:
print("3")
elif x == 4:
print("4")
您应该在 循环之前初始化 sounds
:
def playsound():
sounds = [1,2,3,4]
while True:
t = random.randrange(len(sounds)-1)
...
是否可以 运行 一个循环,它选择一个随机列表项,但不能连续两次选择相同的项。
我已经尝试 运行 在一个线程中使用 Clock.schedule_interval
我不确定如何同时保持循环和 GUI 运行ning。
当间隔结束时,它会“重置”循环。因此可以再次选择相同的项目。
def toggle(self):
self.start()
i = self.root.ids.interval.text #interval
i = int(i)
Clock.schedule_interval(self.playsound, i)
def playsound(self, *args):
while True:
sounds = [1,2,3,4]
t = random.randrange(len(sounds)-1)
sounds.append(sounds.pop(t))
x = (sounds[-1])
if x == 1:
print("1")
elif x == 2:
print("2")
elif x == 3:
print("3")
elif x == 4:
print("4")
您应该在 循环之前初始化 sounds
:
def playsound():
sounds = [1,2,3,4]
while True:
t = random.randrange(len(sounds)-1)
...