【发布时间】:2016-10-09 10:12:52
【问题描述】:
我正在寻找一个程序,在 8x8 网格上随机生成硬币。我已经创建了两个列表(一个用于 X 坐标的列表和一个用于 Y 坐标的列表)。在这些列表中,两个坐标不能相同。很难解释,所以这里是我的例子:
[1, 7, 4, **6**, 9, 2, 3, **6**, 8, 0] (list for the x co-ordinate)
[9, 3, 3, **1**, 2, 8, 0, **1**, 6, 1] (list for the y co-ordinate)
因此,创建了两个列表。但是(6,1) 出现了两次。我不想要这个。那么,我将如何在我的代码中允许这一点,以确保忽略这一点并将数字重新生成为不同的坐标?我的代码如下,我真的不知道如何实现这样的系统!
def treasurePro():
global coinListX, coinListY
coinListX = []
coinListY = []
for x in range(10):
num = randint(0,8)
coinListX.append(num)
print(coinListX)
for x in range(10):
num = randint(0,8)
if num == 0 and coinListX[x] == 0:
treasurePro() #goes back to the beginning to restart.
else:
coinListY.append(num)
print(coinListY)
【问题讨论】:
标签: python arrays list python-3.x