要阻止再次使用同一号码,请执行以下操作:
from random import choice
blacklisted_numbers = [] #this might need to be global if you want to use this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350,260), (600, 260), (100, 80),(350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
#now its out of the loop so its not blacklisted
#Code all of your stuff
blacklisted_numbers.append(number)
总结一下,我的想法是,如果你创建一个空数组,你可以把所有使用过的随机_位置附加在那里,并给数字分配一个随机选择,当它到达while循环时,如果第一个赋值不在那里,循环将不会运行,它将运行你的代码,然后在所有这些之后,你将元组列入黑名单。如果这不是你要问的问题,请进一步澄清